0

I was looking to plot the Fourier Series values, in time, for k = 0,1,2,3.

The goal is to plot each signal separately, then a sum of all four plots. I am not calculating the coefficients.

I am looking to expand this to sampling and how the signal breaks down when the sampling rate is less than twice the max frequency. So, a manual approach I take. No sympi or other package.

I can plot when creating the signals and plots manually. (Though I think there's something off with this plot)

I am running into trouble when trying to create a loop. Any help is appreciated.

Manually plotting the sum only:

import matplotlib
import scipy
import matplotlib.pyplot as plt
import numpy as np

#Set sample size, rate and an operating frequency
start = 0
stop = 2 * np.pi
step = 0.001
T = 1
f = 1 / T

#For Simplicity, Set Fourier coefficients to One
Ak = 1
Bk = 1

#Set x values and radian frequencies
t = np.arange(start, stop, step)
#radian values
w0 = 2* np.pi * f * 0 #Numbers representing series increments
w1 = 2* np.pi * f * 1
w2 = 2* np.pi * f * 2
w3 = 2* np.pi * f * 3

y0 = (Ak * np.cos(w0 * t)) #B_0 = 0
y1 = (Ak * np.cos(w1 * t) + Bk * np.sin(w2 * t))
y2 = (Ak * np.cos(w2 * t) + Bk * np.sin(w2 * t))
y3 = (Ak * np.cos(w3 * t) + Bk * np.sin(w3 * t))

y = y0 + y1 + y2 + y3

plt.plot(t, y)
plt.show

Creating a for loop, setting a list, and using sum(); I can plot each signal and the sum:

#Set x values and radian frequency
t = np.arange(start, stop, step)

y = []
w = []

for k in range(0, 4): 
  w = 2 * np.pi * f * k #Numbers representing series increments in range
  yn = (Ak * np.cos(w * t) + Bk * np.sin(w * t))
  y.append(yn)

  plt.plot(t, yn)
  plt.show
  plt.pause(0.05)
  k += 1

plt.plot(t, sum(y))
plt.show

But the plot is backwards. What did I do wrong, and is there a better way to do this?

4

0 回答 0