Plotting specific range - amplitude peak

Hi, I have this data, and what I am trying to do is plot just the first peak (shown inside the rectangle) of the amplitude plot. I work with different data, so I am looking for a way to do it automatically. Any ideas?
It is more like 'giving a zoom' in that specific range. I am using the following the code to plot the data.
format long g
fid2 = fopen('FarReceiver.csv');
if fid2 > 0
data = textscan(fid2,'%s %s %f %f %f','Delimiter',',','HeaderLines',1);
fclose(fid2);
end
Timestamp2 = strcat(data{:,2});
Timeformat2=datenum(Timestamp2,'HH:MM:SS.FFF');
ConvertTime2 = (Timeformat2 * 86400000);
b = ConvertTime2(1);
TimestampNum2 = (ConvertTime2 - b);
amplitude2 = [data{:,5}]';
Amplitude_max2 = max(abs(amplitude2));
ampfar = (amplitude2/Amplitude_max2);
z = mean(ampfar);
amp2 = (ampfar - z);
plot(TimestampNum2, amp2)
Thank you very much!

 Accepted Answer

If you have the Signal Processing Toolbox, use the findpeaks function.
This is written to be specific to the data you attached, so I don’t know how well it would generalise to your other signals. It is a way to automate your isolation of the first impulse.
This replaces your plot call in your code:
[Pks,Locs] = findpeaks(amp2, 'MinPeakHeight',0.2, 'MinPeakDistance',50);
figure(1)
plot(TimestampNum2, amp2)
hold on
plot(TimestampNum2(Locs(1)), Pks(1), '^r')
hold off
figure(2)
plot(TimestampNum2(Locs(1)-5:Locs(2)-5), amp2(Locs(1)-5:Locs(2)-5))
grid
You may need to experiment with it to get the result you want.

4 Comments

Hey Strider, thank you very much. I've been experimenting with it to get the results, and I also have been studying the find peaks function, but I still don't understand the Locs plot:
plot(TimestampNum2(Locs(1)-5:Locs(2)-5), amp2(Locs(1)-5:Locs(2)-5)
I know the Locs finds the indices, but I don't get the idea of the plot.
Thank you in advance.
My pleasure.
The plot is simply to confirm that the ‘Locs’ returned by findpeaks contain the time and amplitude information you want. The offsets of -5 correct for the peaks not being the exact start of the impulses, but the location of the first peak. Adjust the offsets to provide exactly the result you want. The plot lets you see the results of changing the offsets. It is only necessary in that context.
I often do diagnostic plots and other output to be certain my code is doing what I want it to do, and in this instance, to illustrate what it does.
Thank you very much. Have a good day!
As always, my pleasure.
You, too!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!