Change Symbol on Valleys

1 回表示 (過去 30 日間)
Lisa Macewen
Lisa Macewen 2022 年 1 月 25 日
回答済み: Image Analyst 2022 年 1 月 25 日
I have this code where the goal was to plot the sampled signal and then find the peaks and valleys. I have this so far and were able to get 2 figures (one of the signal, and one of the peaks and valleys). What I'm stuck on is an added bonus where it asks to change the code to make the valleys with a 'o' symbol. Any ideas?
fs=100; % Sampling frequency (samples per second)
dt = 1/fs; % seconds per sample; the sampling period
t=0:dt:10;
x = 5*sin(2*pi*5*t)+3*cos(2*pi*7*t);
figure(3);
plot(t,x);
xlabel('Time(sec)');
ylabel('x(t)');
title('Signal x(t)');
grid on
[peak_positive,loc_positive] = findpeaks(x); % peaks
[peak_negative,loc_negative] = findpeaks(-x); % valleys
v1 = [peak_positive';-peak_negative']; % positives and negatives in vector form
v2 = [loc_positive';loc_negative'];
peak = [v1 v2];
figure(4);
plot(t,x,'k-');
hold on;
plot(t(peak(:,2)),peak(:,1),'k*');
hold off
title('Peaks & Valleys of x(t)');
xlabel('Time(sec)');
ylabel('x(t)');
title('Peaks & Valleys of x(t)');
grid on

回答 (1 件)

Image Analyst
Image Analyst 2022 年 1 月 25 日
Try this:
fs=100; % Sampling frequency (samples per second)
dt = 1/fs; % seconds per sample; the sampling period
t=0:dt:10;
x = 5*sin(2*pi*5*t)+3*cos(2*pi*7*t);
figure(3);
plot(t,x);
xlabel('Time(sec)');
ylabel('x(t)');
title('Signal x(t)');
grid on
[peak_positive,loc_positive] = findpeaks(x); % peaks
[peak_negative,loc_negative] = findpeaks(-x); % valleys
v1 = [peak_positive';-peak_negative']; % positives and negatives in vector form
v2 = [loc_positive';loc_negative'];
peak = [v1 v2];
figure(4);
plot(t,x,'k-');
hold on;
% plot(t(peak(:,2)),peak(:,1),'k*');
plot(t(loc_positive), peak_positive,'k*');
peak_negative = -peak_negative;
plot(t(loc_negative), peak_negative,'ro', 'LineWidth', 2);
hold off
title('Peaks & Valleys of x(t)');
xlabel('Time(sec)');
ylabel('x(t)');
title('Peaks & Valleys of x(t)');
grid on

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by