0
votes

I am trying to plot the frequency spectrum of a sinewave with frequency 77.5 kHz and amplitude 2. Only the zero-point is not correct, it is shifted to the left. My code:

f1=77.5e3; % frequency of the sinewave 
a1=2; % amplitude of the sinewave 

Fs = 1.55e6; % sampling frequency
dt=1/Fs; % step size 
maxtime = 5*(1/f1);
t=0:dt:maxtime; % time interval in which we want to plot 

x=a1*sin(2*pi*f1*t); % the values for the sinewave

N=length(t);  % this is how many samples we have in the time-domain
X=fft(x)/N; 
X=fftshift(X);
f=[-N/2:1:N/2-1]*Fs/N;  % creates a frequency axis 

figure(1)
plot(f,abs(X))
title('Magnitude Spectrum of x(t)')
xlabel('Frequency [Hz]')
ylabel('|X(f)|')

When I run this code I get an incorrect frequency spectrum. Can anyone help me out?

Edit: the figure I get when running this code: Incorrect spectrum I get

Besides the incorrect zero-point I also get an incorrect frequency when I count it out myself from the plot. I'm just not sure how I should plot such a sinewave with frequency 77.5kHz, amplitude 2 and sampling frequency 1.55 MHz

1
Can you post an example?Oliver Charlesworth
@OliverCharlesworth I updated my questionB_s
why do you use fftshift?? why not just fft(x,N)/N?Rashid
I am new to Matlab and this was in the explanation I found. Isn't fftshift necessary?B_s
No it is not, just use fft(x,N)Rashid

1 Answers

1
votes

Your code is correct as it is. But your signal, once made periodic, is not just a sine wave (there is a discontinuity, because the 1st and last samples of x are the same).

You can try removing 1 sample at the end:

t=0:dt:maxtime; % time interval in which we want to plot 
t = t(1:end-1);

Now the peak is at f1.