Hello. Would you mind if you could assist me in my algorithm I did. I have to find every peak values generated randomly from walking patterns. For example I have subject one walk for certain amount of time, and I'm having sampling rate 100 samples per second. However the subject may take longer or less than the periodicity of sampling rate, however that is fine - this is not the issue. But my algorithm which is slop base, is not efficiently working to find every peak - Not the local maxima please. The txt file has numbers of one big vectors (say 1X3000 number) and reading row by row. Thank you so much in advance.
x=load('girl_1.txt');
tmax= length(x) ;
x4 = x(1:5:end);
t1_n = 1:5:tmax;
x1_n_ref=0;
k=0;
for i=1:length(t1_n)
if x4(i)>140
if x1_n_ref-x4(i)<0
x1_n_ref=x4(i);
alpha=1;
elseif alpha==1 && x1_n_ref-x4(i)>0
k=k+1;
peak(k)=x1_n_ref;
peak_time(k) = t1_n(i);
alpha=2;
end
else
x1_n_ref=0;
end
end
figure(1) hold on grid plot(t1_n, x4,'b');

1 件のコメント

Ahmed Mostfa
Ahmed Mostfa 2016 年 2 月 23 日
I have down sampling by 5, but it should be an issue, - I'm not catching all my peak values.

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2016 年 2 月 23 日

0 投票

You forgot to attach 'girl_1.txt' so we can't try anything. Anyway, to find the peaks, what if you just use findpeaks()? Or else threshold and useregionprops() to find the weight centroid of each peak.

5 件のコメント

Ahmed Mostfa
Ahmed Mostfa 2016 年 2 月 23 日
Ok, now I did add the txt file. the numbers are separated by equal space. I don't know about findpeaks or usergionprops. Are these already set functions that can find the peaks values? I wish yes.
Image Analyst
Image Analyst 2016 年 2 月 23 日
findpeaks() is in the Signal Processing Toolbox. regionprops() is in the Image Processing Toolbox. Type "ver" to see which toolboxes you have.
I'm not exactly sure what you want. Do you want the (x,y) location of each peak in the signal? Or something else? Anyway, try findpeaks() first and see what happens.
Ahmed Mostfa
Ahmed Mostfa 2016 年 2 月 23 日
Yes I want the x,y location of each peak. the "x" value will tell the time of that peak occurred. I mean every peak. Okay. I will study about findpeaks(). I feel this is correct one. Please bear with me if I couldn't make it. I have no one to help me out. Thank you so much.
Ahmed Mostfa
Ahmed Mostfa 2016 年 2 月 23 日
It keeps showing me this error:
Error in ==> findpeaks at 43
[X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(X,varargin{:});
Error in ==> loading_data at 85
findpeaks(relNums,year);
I used the original data of matalb example, and it shows the above error.
load sunspot.dat
year=sunspot(:,1);
relNums=sunspot(:,2);
findpeaks(relNums,year);
xlabel('Year');
ylabel('Sunspot Number')
title('Find All Peaks');
Although I have
Signal Processing Toolbox Version 6.15 (R2011a)
Image Processing Toolbox Version 7.2 (R2011a)
Would you mind to tell me why please? thank you so much.
Explorer
Explorer 2016 年 2 月 23 日
I was also getting similar error till I upgraded my MATLAB to MATLAB R2015b.

サインインしてコメントする。

その他の回答 (1 件)

Star Strider
Star Strider 2016 年 2 月 23 日

1 投票

In the documentation for the R2011a version of findpeaks function, it only takes one data argument, and the name-value pair arguments. (Also check to be sure if you have ‘sunspot.mat’.)
You have to use the documentation for the release you have. The online documentation is always for the latest release, and there can be significant version differences.

7 件のコメント

Ahmed Mostfa
Ahmed Mostfa 2016 年 2 月 23 日
It still not useful much unless I couldn't understand it better. My slop algorithm above states that once the difference between two adjacent is equal negative then that is my peak, - it works better than findpeaks function that i just set as following. if you could suggest or assist me, i would appreciate it.
aa= findpeaks(x1_n,'MinPeakHeight',140, 'MinPeakDistance',30);
Ahmed Mostfa
Ahmed Mostfa 2016 年 2 月 23 日
x=load('girl_1.txt');
tmax=length(x);
t1_n=0:tmax-1;
x1_n=x(1:end);
aa= findpeaks(x1_n,'MinPeakHeight',140, 'MinPeakDistance',30);
Star Strider
Star Strider 2016 年 2 月 23 日
I would use findpeaks with two outputs:
[pks,locs] = findpeaks(data)
to get the indices of the peaks as well as the values. You can use the ‘locs’ variable to give you the values of your independent variable that correspond to the peak values in ‘pks’.
If your algorithm produces better results for you than findpeaks, go with what works best for you.
Ahmed Mostfa
Ahmed Mostfa 2016 年 2 月 23 日
Still struggling with my algorithm above. findpeaks didn't work well. The problem in the algorithm above is that some times it works and some time when I load different data, it is not catching all the peaks.
Star Strider
Star Strider 2016 年 2 月 23 日
You will have to make the findpeaks call adaptive to your data.
Consider something like this:
D = load('Ahmed Mostfa girl2.txt','-ascii');
Dmax = max(D);
[pks,locs] = findpeaks(D, 'MinPeakHeight',Dmax/2, 'MinPeakDistance',20);
figure(1)
plot(D)
hold on
plot(locs, pks, '*r')
hold off
grid
This works on that particular data set. It only picks up peaks that are half the maximum peak. The MinPeakDistance would be set as a constant and would eliminate peaks created by sampling and other broadband noise. I don’t know what your other data are, so I cannot code any particular alternative adaptive method.
Ahmed Mostfa
Ahmed Mostfa 2016 年 2 月 24 日
Thank you sir, that is helpful, I also fixed my algorithm to be same as your code provided. Thanks again.
Star Strider
Star Strider 2016 年 2 月 25 日
My pleasure.
If my Answer solved your problem, I would have asked you to Accept it.

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by