Plotting amplitude spectrum of a signal

212 Ansichten (letzte 30 Tage)
prodeje
prodeje am 23 Apr. 2020
Beantwortet: Sady am 25 Okt. 2023
So I need to generate a segment of 95 Hz sine wave for the duration of 35 ms, with 3.5 kHz sampling rate and display it in 2 graphs, time domain and amplitude spectrum.
I am struggling with amplitude spectrum, I imagine function fft is required, but how should it be used in this case?
Thanks in advance!
Here is my code below for representation in time domain:
Fs = 3500; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.35; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
Fc = 95; % hertz
x = cos(2*pi*Fc*t);
% Plot the signal versus time:
plot(t,x);
xlabel('Time(S)');
title('Signal versus Time');

Antworten (3)

Ameer Hamza
Ameer Hamza am 23 Apr. 2020
Bearbeitet: Ameer Hamza am 23 Apr. 2020
Try the following code. Also see the first example on the documentation page of ff() for more details.
Fs = 3500; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.35; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
Fc = 95; % hertz
x = cos(2*pi*Fc*t);
% Plot the signal versus time:
subplot(2,1,1);
plot(t,x);
xlabel('Time(S)');
title('Signal versus Time');
subplot(2,1,2);
f = Fs*linspace(0, 1/2, floor(numel(x)/2));
fr = fft(x);
fr = abs(fr)/numel(x);
fr = fr(1:floor(end/2));
fr(2:end-1) = 2*fr(2:end-1);
plot(f, fr);
xlabel('Time(S)');
title('Frequency Response');
  2 Kommentare
Ian Cobb
Ian Cobb am 10 Feb. 2023
shouldnt the x axis of the second plot be frequency in Hz?
Paul
Paul am 10 Feb. 2023
Yes, it should

Melden Sie sich an, um zu kommentieren.


Mehmed Saad
Mehmed Saad am 23 Apr. 2020

Sady
Sady am 25 Okt. 2023
Fs = 3500; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.35; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
Fc = 95; % hertz
x = cos(2*pi*Fc*t);
% Plot the signal versus time:
subplot(2,1,1);
plot(t,x);
xlabel('Time(S)');
title('Signal versus Time');
subplot(2,1,2);
f = Fs*linspace(0, 1/2, floor(numel(x)/2));
fr = fft(x);
fr = abs(fr)/numel(x);
fr = fr(1:floor(end/2));
fr(2:end-1) = 2*fr(2:end-1);
plot(f, fr);
xlabel('Time(S)');
title('Frequency Response');

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by