# How is sampling affecting this sine wave?

I am trying to create a sine wave in python, but when I graph it, it looks like this:

here is the code I used to make the signal:

c_freq = 1700 * 1000
fs = c_freq * 2
secs = 3
x = fs*secs

t = np.linspace(0, secs, x)
carrier = np.sin(t * c_freq * 2 * np.pi)


Why is the amplitude changing throughout the signal and how can I fix this?

• We've had fun with this one. Jun 16 at 4:40
• I'm sorry, I'm not sure I get it. What should I do to fix this problem? Jun 16 at 5:01
• increase your sampling rate. Jun 16 at 5:16
• Why is it a problem? The graph looks funny but everything you do with the data will stay work. Is this a cosmetic or technical issue ? Jun 16 at 11:51

Because you are sampling it at oh so slightly less than c_freq/2. Your linspace call generates x points that are evenly spaced between 0 and 3, with a spacing between them of 3 * c_freq * 2 / (3 * c_freq * 2 + 1). This means that the phase that gets calculated is $$\begin{bmatrix}0, \pi-\epsilon, 2\pi - 2\epsilon,\cdots,(n-1)\pi-\pi+\epsilon, n \pi - \pi\end{bmatrix}$$ -- and that gives the result you see, with alternating numbers on a half-rotation envelope.
(Why does it do that? Work it out -- it'll be good for you. Start by looking at how linspace actually behaves, i.e. linspace(0, 10, 10)).