Plotting one cycle of a wave

[sig, fs] = audioread(x);
modfreq = 4;
depth = 100;
a = depth/200;
offset = 1 - a;
len = 1:length(sig);
phasor = a*sawtooth(2*pi*len*(modfreq/fs)) +offset;
I'm generating a wave for a tremolo. I know how to plot the entire wave(at the length of the input signal), however I would like to be able to plot just a single cycle.
I found an answer in a previous question:
fs = 512; % Sampling frequency (samples per second)
dt = 1/fs; % seconds per sample
StopTime = 0.25; % seconds
t = (0:dt:StopTime)'; % seconds
F = 60; % Sine wave frequency (hertz)
data = sin(2*pi*F*t);
plot(t,data)
%%For one cycle get time period
T = 1/F ;
% time step for one time period
tt = 0:dt:T+dt ;
d = sin(2*pi*F*tt) ;
plot(tt,d) ;
However, it generates a second wave at a shorter length. I would like to be able to plot just one cycle of the original wave without creating a second.
Does anyone know how to do this?
Thanks in advance.

1 件のコメント

madhan ravi
madhan ravi 2018 年 12 月 18 日
you want to restrict the range ? that's what you mean?

サインインしてコメントする。

回答 (2 件)

Omer Yasin Birey
Omer Yasin Birey 2018 年 12 月 18 日

0 投票

I believe axis() would work. I wrote an example code with a signal which has cycles.
x = 1:100;
signal = (1-cos(2*pi*0.01*x)).*sin(2*pi*0.15*x);%signal with cycles
plot(signal,'r-')
[peak,locs] = findpeaks(-signal); % Find Minimas
%you can use loop to plot every single cycle. it is just for the first one
firstInd = locs(1);%use a minima and the next one to find cycle limits
lastInd = locs(2);
%limit the plot with the start and end of the both x and y axes
axis([firstInd lastInd -peak(1) peak(2)])

2 件のコメント

William Chambers
William Chambers 2018 年 12 月 18 日
Thanks for this, I managed to get it working in my test script, but when I try to implement it within the app designer it doesn't work.
T = 1/modfreq ;
dt = 1/fs;
tt = 0:dt:T+dt ;
plot(app.UIAxes, len, wave)
axis([0 length(tt) 0 1])
I've left out some parts of the code in this snippet, but you should get the idea of what I'm trying to do. When I run the app desginer app, it just plots the unlimited range plot (all cycles) and a random blank graph.
Omer Yasin Birey
Omer Yasin Birey 2018 年 12 月 18 日
編集済み: Omer Yasin Birey 2018 年 12 月 18 日
I think length(tt) is the problem here. Because you start from 0 to length(tt), which means the whole range of signal and tt might have a very long sequence. Finding the 2 minimas, left and right, which are bounding a cycle still seems the possible solution to me.
T = 1/modfreq ;
dt = 1/fs;
tt = 0:dt:T+dt ;
[peak,locs] = findpeaks(-signal);
for i = 1:2:length(locs)-2
figure
firstInd = locs(i);
lastInd = locs(i+1);
plot(app.UIAxes, len, wave)
axis([firstInd lastInd -peak(i) peak(i+1)])
end
This code above should plot all the cycles seperately.

サインインしてコメントする。

Rohan Basak
Rohan Basak 2020 年 9 月 20 日
編集済み: Walter Roberson 2020 年 9 月 20 日

0 投票

T = 1/modfreq ;
dt = 1/fs;
tt = 0:dt:T+dt ;
[peak,locs] = findpeaks(-signal);
for i = 1:2:length(locs)-2
figure
firstInd = locs(i);
lastInd = locs(i+1);
plot(app.UIAxes, len, wave)
axis([firstInd lastInd -peak(i) peak(i+1)])
end

1 件のコメント

Walter Roberson
Walter Roberson 2020 年 9 月 20 日
Rohan Basak:
Why are you create a new figure each time, even though you are plotting on a fixed axes?
Note also that the axis() command you are using is going to apply to the "current" axis, which is going to be a newly generated axes in the newly generated figure.

サインインしてコメントする。

カテゴリ

製品

リリース

R2018a

タグ

質問済み:

2018 年 12 月 18 日

コメント済み:

2020 年 9 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by