Change Symbol on Valleys

7 visualizaciones (últimos 30 días)
Lisa Macewen
Lisa Macewen el 25 de En. de 2022
Respondida: Image Analyst el 25 de En. de 2022
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

Respuestas (1)

Image Analyst
Image Analyst el 25 de En. de 2022
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

Community Treasure Hunt

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

Start Hunting!

Translated by