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

回答(2 个)

Star Strider
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))
period = 0.0022
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))
period = 0.3480
[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')
.

Voss
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))
period = 0.0022
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
idx = 1×61
38 792 1552 2313 2873 3433 4193 4953 5713 6273 6833 7593 8353 9113 9674 10233 10994 11754 12514 13074 13634 14394 15158 15160 15916 16476 17036 17796 18556 19316
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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))
periods = 60×1
0.3770 0.3800 0.3805 0.2800 0.2800 0.3800 0.3800 0.3800 0.2800 0.2800
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
disp(periods(6))
0.3800
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
idx1 = 1×61
38 792 1552 2313 2873 3433 4193 4953 5713 6273 6833 7593 8353 9113 9674 10233 10994 11754 12514 13074 13634 14394 15158 15160 15916 16476 17036 17796 18556 19316
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
idx2 = strfind(isgr,[true false]) % indices where threshold is crossed falling
idx2 = 1×60
600 1400 2200 2800 3400 4000 4800 5600 6200 6800 7400 8200 9000 9600 10200 10800 11600 12400 13000 13600 14200 15000 15159 15800 16400 17000 17600 18400 19200 19800
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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 = 60×1
0.2810 0.3040 0.3240 0.2435 0.2635 0.2835 0.3035 0.3235 0.2435 0.2635
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
periods(6)
ans = 0.2835

类别

Help CenterFile Exchange 中查找有关 Measurements and Feature Extraction 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by