1

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

enter image description here

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?

user57935
  • 11
  • 2

1 Answers1

3

Why is the amplitude changing throughout the signal

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)).

and how can I fix this?

What's wrong with it? What do you want? If you want a pretty sine wave, you need to sample way more than twice the frequency, and everyone defines "pretty" differently, so there's no fixed number. I usually use 100 points per cycle of a sine wave -- but that would get pretty time intensive given that you want to plot three seconds of a 1.7MHz sine wave.

TimWescott
  • 12,704
  • 1
  • 11
  • 25