0
votes

I'm using the following code the plot a sine wave in Matlab,

   Fs = 12000;                       % sampling frequency in Hz
   dt = 1/Fs;                        % seconds per sample
   StopTime = 0.25;                  % seconds
   t = (0:dt:StopTime-dt)';          % seconds

   Fc = 5000;                        % hertz
   x = sin(2*pi*Fc*t);

   figure;                           % Plot the signal versus time:
   plot(t,x);
   xlabel('time (in seconds)');
   title('Signal versus Time');

The code tends to work in general but when I try to plot a 5kHz sine wave, I get the following results:

enter image description here

I know that you need the sampling frequency to be at least 2 times the maximum frequency you want to plot (Nyquist theorem). So why is this happening?

1

1 Answers

4
votes

You are getting aliasing. Your sampling frequency needs to be much, much higher than that the maximum frequency you want to plot to get a "smooth" plot.

With a 5 kHz sine wave, you need at a sampling frequency of at least 50 kHz, and even then that's not very much (only 10 data points per period). 100 kHz would give you 20 data points per period, which might start to look OK:

enter image description here