How to calculate the period of the 6th wave in my data when I dont have the equation of the wave
3 次查看(过去 30 天)
显示 更早的评论
Hello, I have relatively little experience using matlab. I am attempting to calculate the period of the 6th wave in my data set. I have graphed it and used the find peaks function, but I am unsure as how to select specific peaks in order to find their specific periods.
This is the code I have so far. And the mean period it calculates also seems to be incorrect.
load 20250123.mat
time = data(:, 4);
voltage = data(:, 5);
[~,peaklocs] = findpeaks(voltage,time);
period = mean(diff(peaklocs))
I uploaded the data I am using; columns 4 and 5
0 个评论
回答(2 个)
Star Strider
2025-3-6
I’m not sure what the ‘6th wave’ is. The problem is that you need to give findpeaks a bit more information.
Here, I added MinPeakProminence as an additional name-value pair argument in order to return only the more prominent peaks.
What do you want to do?
load 20250123.mat
time = data(:, 4);
voltage = data(:, 5);
[vpeaks,peaklocs] = findpeaks(voltage,time);
period = mean(diff(peaklocs))
figure
plot(time, voltage)
hold on
plot(peaklocs, vpeaks, '|r')
hold off
xlabel('time')
ylabel('voltage')
xlim([0 3])
title('Original')
[~,peaklocs] = findpeaks(voltage,time, MinPeakProminence=2);
period = mean(diff(peaklocs))
[peaks,locs] = findpeaks(voltage, MinPeakProminence=2);
figure
plot(time, voltage)
hold on
plot(time(locs), voltage(locs), 'vr')
hold off
grid
xlabel('time')
ylabel('voltage')
xlim([0 3])
title('With MinPeakProminence')
figure
plot(time, voltage)
hold on
plot(time(locs), voltage(locs), 'vr')
hold off
grid
xlabel('time')
ylabel('voltage')
.
0 个评论
Voss
2025-3-6
编辑:Voss
2025-3-6
load 20250123.mat
time = data(:, 4);
voltage = data(:, 5);
[pks,peaklocs] = findpeaks(voltage,time);
period = mean(diff(peaklocs))
If you plot those points found by findpeaks, you'll see it's way more than one per 'wave' in the signal (each green circle is a peak):
figure
subplot(2,1,1)
hold on
plot(time,voltage,'.-')
plot(peaklocs,pks,'og')
xlim('tight')
title('all time')
subplot(2,1,2,copyobj(gca(),gcf()))
xlim([0,2.1])
title('zoomed to beginning')
If the objective is to find the periods of the waves in the signal, one approach is to find the times when the signal rises above some threshold (i.e., goes from being at-or-below the threshold at one sample time then above the threshold at the next sample time), then the periods are the differences between consecutive such times. [I'll use the mean signal value as the threshold.]
vmean = mean(voltage);
isgr = voltage(:).' > vmean;
idx = strfind(isgr,[false true]) % indices where threshold is crossed rising
figure('Position',[10 10 800 800])
subplot(3,1,1)
hold on
plot(time,voltage,'.-')
plot(time(idx),voltage(idx),'og')
xline(time(idx),'--g')
yline(vmean,'--b')
xlim('tight')
title('all time')
subplot(3,1,2,copyobj(gca(),gcf()))
xlim([0,2.1])
title('zoomed to beginning')
subplot(3,1,3,copyobj(gca(),gcf()))
xlim([19,20.9])
title('zoomed to end')
periods = time(idx(2:end)) - time(idx(1:end-1))
disp(periods(6))
If you want to define a wave period not as rise-time to rise-time but rise-time to fall-time then:
vmean = mean(voltage);
isgr = voltage(:).' > vmean;
idx1 = strfind(isgr,[false true]) % indices where threshold is crossed rising
idx2 = strfind(isgr,[true false]) % indices where threshold is crossed falling
figure('Position',[10 10 800 800])
subplot(3,1,1)
hold on
plot(time,voltage,'.-')
plot(time(idx1),voltage(idx1),'og')
plot(time(idx2),voltage(idx2),'or')
xline(time(idx1),'--g')
xline(time(idx2),'--r')
yline(vmean,'--b')
xlim('tight')
title('all time')
subplot(3,1,2,copyobj(gca(),gcf()))
xlim([0,2.1])
title('zoomed to beginning')
subplot(3,1,3,copyobj(gca(),gcf()))
xlim([19,20.9])
title('zoomed to end')
n = min(numel(idx1),numel(idx2));
if n > 0 && idx1(1) > idx2(1)
idx2(1) = [];
end
periods = time(idx2(1:n)) - time(idx1(1:n))
periods(6)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Measurements and Feature Extraction 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!