plot a inear sweep & stepped sweep

Hello,
I am try to plot a linear sweep & stepped sweep. (The attached picture)
condition:
Freauency range from 20Hz-20kHz
Time from 0-100ms
How the code can do it ? Tnanks!

4 Commenti

Rik
Rik il 1 Lug 2023
What have you tried so far? And do you only want to have the plot, or do you need the time,frequency pairs at some resolution?
Hi,
This my code so far.
fc1=20; %start frequency (Hz)
fc2=20e3; %end frequency (Hz)
dt=25e-6; %time step (s)
A0=0; %mean value of signal
A1=1; %amplitude of oscillation
Tend=1; %chirp duration (s)
t=0:dt:Tend; %time vector
theta=2*pi*(fc1*t+(fc2-fc1)*t.^2/(2*Tend)); %theta vector
plot(t, theta)
and I want use the linear function is f(t)=fc1+(fc2-fc1)/t
and i want use the stepped function is f(t)=fc1+(fc2-fc1)n/N-1
N is the stepped and n is the frequency index from 0~N-1
Would the built-in chirp function help you?
help chirp
CHIRP Swept-frequency cosine generator. Y = CHIRP(T) generates samples of a swept-frequency signal at the time instances defined in array T. By default, the instantaneous frequency at time 0 is 0, and the instantaneous frequency one second later is 100 Hz. Y = CHIRP(T,F0,T1,F1) generates samples of a linear swept-frequency signal at the time instances defined in array T. The instantaneous frequency at time 0 is F0 Hz. The instantaneous frequency F1 is achieved at time T1. Y = CHIRP(T,F0,T1,F1,method) specifies alternate sweep methods. Available methods are 'linear', 'quadratic', and 'logarithmic'; the default is 'linear'. For a logarithmic-sweep, F0 must be greater than or equal to 1e-6 Hz, which is also the default value. Y = CHIRP(T,F0,T1,F1,method,PHI) specifies an initial phase PHI in degrees. By default, PHI = 0. Y = CHIRP(T,FO,T1,F1,'quadratic',PHI,'concave') generates samples of a quadratic swept-frequency chirp signal whose spectrogram is a parabola with its concavity in the positive frequency axis. Y = CHIRP(T,FO,T1,F1,'quadratic',PHI,'convex') generates samples of a quadratic swept-frequency signal whose spectrogram is a parabola with its convexity in the positive frequency axis. Y = CHIRP(...,sigtype) specifies sigtype as 'real' or 'complex'; the default is 'real'. When sigtype is set to 'real', CHIRP generates real chirp signals. When set to 'complex', CHIRP generates complex chirp signals. EXAMPLE 1: Compute the spectrogram of a linear chirp. t = 0:0.001:2; % 2 s at 1 kHz sample rate y = chirp(t,0,1,150); % Start at DC, cross 150 Hz at % t = 1 s spectrogram(y,256,250,256,1E3); % Display the spectrogram EXAMPLE 2: Compute the spectrogram of a quadratic chirp. t = -2:0.001:2; % +/-2 s at 1 kHz sample rate y = chirp(t,100,1,200,'q'); % Start at 100 Hz, cross 200 Hz at % t = 1 s spectrogram(y,128,120,128,1E3); % Display the spectrogram EXAMPLE 3: Compute the spectrogram of a convex quadratic chirp t = 0:0.001:1; % 1 s at 1 kHz sample rate fo = 25; % Start at 25 Hz, f1 = 100; % go up to 100 Hz y = chirp(t,fo,1,f1,'q',[],'convex'); spectrogram(y,256,200,256,1000); % Display the spectrogram. EXAMPLE 4: Compute the spectrogram of a concave quadratic chirp t = 0:0.001:1; % 1 s at 1 kHz sample rate fo = 100; % Start at 100 Hz, f1 = 25; % go down to 25 Hz y = chirp(t,fo,1,f1,'q',[],'concave'); spectrogram(y,256,200,256,1000); % Display the spectrogram. EXAMPLE 5: Compute the spectrogram of a logarithmic chirp t = 0:0.001:10; % 10 s at 1 kHz sample rate fo = 10; % Start at 10 Hz, f1 = 400; % go up to 400 Hz y = chirp(t,fo,10,f1,'logarithmic'); spectrogram(y,256,200,256,1000); % Display the spectrogram EXAMPLE 6: Compute a complex-valued linear chirp t = 0:0.001:2; % 2 s at 1 kHz sample rate fo = -50; % Start at -50 Hz, t1 = 1; f1 = 200; % cross 200 Hz at t = 1 s y = chirp(t,fo,t1,f1,'complex'); spectrogram(y,256,200,256,1000,'centered'); % Display the spectrogram See also GAUSPULS, SAWTOOTH, SINC, SQUARE. Documentation for chirp doc chirp
Thanks for your help. But i want to plot like the picture. one line is linear other one is stepped.

Accedi per commentare.

Risposte (1)

t=0:8:100;
f=linspace(20,2000,length(t));
stairs(t,f);
xlabel('Time');
ylabel('Frequency');
hold on
plot(0:100,linspace(20,2000,101))
hold off
If the use of the inbuilt stair function is not allowed, modify the code according to the requirements, finding alternative methods such as the step function or other approaches can be done.

2 Commenti

Thank so much & thank all of your help
Now i have the other queston.
I want to plot a stepped frequency singal (like the picture)
(Time during 0 to 1 sec)

Accedi per commentare.

Richiesto:

il 1 Lug 2023

Commentato:

il 2 Lug 2023

Community Treasure Hunt

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

Start Hunting!

Translated by