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).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…