2

I have to plot the signal m(t) = ASin(2pi1000t) and I am using the following Matlab code to do it.

Fm=1000;%1 kHz

Fs = 2*Fm;

t = 0:1/Fs:10; % ( that is, time will run from 0 to 10 with a sample at every 1/2000th second )
Am=1;%amplitude
m=Am*sin(2*pi*Fm*t);

figure(1)
plot(t,m)
title('Message signal')
xlabel('Time in Seconds') 
ylabel('m(t)') 

However, I am getting the plot as

enter image description here

But on the other hand, if I use the following code,

Fm=1000;%1 kHz

t=linspace(0,10,2000);
Am=1;%amplitude
m=Am*sin(2*pi*Fm*t);

figure(1)
plot(t,m)
title('Message signal')
xlabel('Time in S')

enter image description here

I am getting a proper sin wave. What's actually happening here? What is wrong with the first code?

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147

2 Answers2

3

The problem with the first code snippet is that the sampling period is exactly half the period of the sinusoid. Due to the specific sampling instants that you use, you always sample the signal at its nulls. That's why you get values close to 0 (they are not exactly 0 because of the numerical inaccuracy inherent to floating-point arithmetic).

In the second code snippet, since linspace is inclusive at its end points, the sampling period is slightly different. So you don't have the same problem as above, and you do get a sinusoid. However, you have a different problem which is now made evident, namely aliasing due to insufficient sampling. Observe how the frequency of the plotted sinusoid is very different (much smaller) from what it should be.

The solution to both problems is to increase the sample rate. According to the Nyquist criterion, a sample rate at least twice the maximum signal frequency would be enough to reconstruct the original signal. But that does not mean that directly plotting the samples taken at that rate will produce a graph resembling the signal. For that you need a factor significantly greater than 2. Also, avoid choosing the sample rate as an integer multiple of the sinusoid frequency, to prevent problems caused by the sampling process being "coupled" to the signal variations as in your first snippet.

So, in your code, try for instance Fs = 100/3*Fm (you may need to zoom in to see the signal properly).

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • reducing the sampling period to a value smaller than the sinusoid period also means that increasing sampling frequency to a value greater than the sinusoid frequency right? So can I simply use Fs = 3*Fm as sampling frequency? –  Jan 25 '21 at 18:27
  • 1
    @MuhammedRoshan Any `Fs` above `2*Fm` would do according to the Nyquist criterion. In practice, if you plot the samples directly you need a factor greater than `2` to have the plot resemble the signal. Also, I suggest not to use an interger multiple of `Fm`, to avoid things like in the first graph. So try something like `Fs = 100/3*Fm` for instance (and zoom in to see the signal) – Luis Mendo Jan 25 '21 at 18:34
0

Fm=1000 #%1 kHz

here is the python version

import math
import matplotlib.pyplot as plt
Fs = 2*Fm

#t = 0:1/Fs:10  #( that is, time will run from 0 to 10 with a sample at every 1/2000th second )
t=np.linspace(0,10,Fs)

Am=1   #%amplitude
m=[ Am*math.sin(2*math.pi*Fm*value) for value in t]
plt.plot(m)
plt.show()
Golden Lion
  • 3,840
  • 2
  • 26
  • 35