This page outlines how waves are created with harmonics.
Waveform Harmonics |
Source code
The following outlines the Python code used:
import matplotlib.pyplot as plot import numpy as np import sys a1=1.0 a2=0.5 a3=0.3 a4=0.2 a5=0.1 # Saved into 1111.png (see folder) file ='1111' if (len(sys.argv)>1): file=str(sys.argv[1]) if (len(sys.argv)>2): a1=float(sys.argv[2]) if (len(sys.argv)>3): a2=float(sys.argv[3]) if (len(sys.argv)>4): a3=float(sys.argv[4]) if (len(sys.argv)>5): a4=float(sys.argv[5]) if (len(sys.argv)>6): a5=float(sys.argv[6]) Fs = 150.0; # sampling rate Ts = 1.0/Fs; # sampling interval freq = 5; # frequency of the signal t = np.arange(0,1,Ts) y = a1*np.sin(2*np.pi*freq*t) + a2* np.sin(2*np.pi*2*freq*t) + a3* np.sin(2*np.pi*3*freq*t) + a4* np.sin(2*np.pi*4*freq*t) + a5* np.sin(2*np.pi*5*freq*t) n = len(y) # length of the signal k = np.arange(n) T = n/Fs frq = k/T # two sides frequency range frq = frq[range(n//2)] # one side frequency range Y = np.fft.fft(y)/n # fft computing and normalization Y = Y[range(n//2)] fig,myplot = plot.subplots(2, 1) myplot[0].plot(t,y) myplot[0].set_xlabel('Time') myplot[0].set_ylabel('Amplitude') myplot[1].plot(frq,abs(Y),'r') # plotting the spectrum myplot[1].set_xlabel('Freq (Hz)') myplot[1].set_ylabel('|Y(freq)|') plot.savefig(file) plot.show()