SHARE
TWEET

Untitled

a guest Sep 16th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3
  2. import sys
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import wave
  6. import struct
  7. import scipy.io.wavfile
  8. from scipy import signal
  9.  
  10. # generate original square wave
  11. def square_wave(T0=10, Tpulse=2/3, A=1, dur=0.3, fs=44100):
  12.     t = np.linspace(0, dur, fs*dur, False)
  13.     swave = (A/2)*signal.square((2*np.pi*T0*(t+0.1/3)), duty = Tpulse)+0.5
  14.     return swave
  15.  
  16. # calculate amplitudes for spectral signal
  17. def amplitudes(n):
  18.     a = []
  19.     a.append(1/3)
  20.     for i in range (1,n+1):
  21.         a.append( (np.sin((2*np.pi*i)/3)) / (np.pi*i) )
  22.     return a
  23.  
  24. # calculate frequencies of harmonics
  25. def frequencies(n, f0=10):
  26.     f = []
  27.     for i in range(0,n+1):
  28.         f.append(i*f0)
  29.     return f
  30.  
  31. # generate periodic sound
  32. def periodic(t, f, A, phi):
  33.     x = A*(np.cos((2*np.pi*f*t)+phi*np.pi))
  34.     return x
  35.  
  36. # plot two waves on the same graph
  37. def plot_two_waves(y1, y2, n, filename, fs=44100):
  38.     t = np.linspace(0, len(y1)/fs, len(y1), False)
  39.     plt.plot(t, y1)
  40.     plt.plot(t, y2)
  41.     plt.title('Square wave')
  42.     plt.xlabel('Time(s)')
  43.     plt.ylabel('X')
  44.     plt.grid(True)
  45.     plt.ylim(-0.2,1.2)
  46.     plt.savefig(filename)
  47.     plt.show()
  48.  
  49. # plot the spectrum using a lollipop graph
  50. def plot_spectrum(n):
  51.     spectrum = []
  52.     t = np.linspace(-n, n, (n*2)+1, False)
  53.     for i in range (-n,0):
  54.         spectrum.append( (np.sin((2*np.pi*i)/3)) / (np.pi*i) )
  55.     spectrum.append(2/3)
  56.     for i in range (1,n+1):
  57.         spectrum.append( (np.sin((2*np.pi*i)/3)) / (np.pi*i) )
  58.     plt.title('Spectrum')
  59.     plt.xlabel('N')
  60.     plt.ylabel('Xn')
  61.     plt.stem(t,spectrum,use_line_collection=True)
  62.     plt.show()
  63.  
  64. # generate the reconstructed wave
  65. def generate_wave(amps, freqs, phi=0, dur=0.3, fs=44100):
  66.     sum = 0
  67.     t = np.linspace(0, dur, fs*dur, False)
  68.     for x in range (0, len(amps)):
  69.         z = 2*periodic(t, freqs[x], amps[x], phi)
  70.         sum += z
  71.     return sum
  72.  
  73. # normalise to 16 bits
  74. def normalize(y):
  75.     for x in range(0,len(y)):
  76.         y[x] = y[x]/3.2767
  77.         if (y[x] > 1):
  78.             y[x] = 1
  79.     y = np.array(y, dtype='int')
  80.  
  81. def write_sound(y, filename="sound", fs=44100, nchannels=1):
  82.     scipy.io.wavfile.write('filename.wav',fs,y)
  83.  
  84. if __name__ == '__main__':
  85.  
  86.     # get n
  87.     n = int(sys.argv[1])
  88.  
  89.     # get file name
  90.     filename = sys.argv[2]
  91.  
  92.     # create array of amplitudes for spectral components
  93.     amps = amplitudes(n)
  94.  
  95.     # create array of frequencies for spectral components
  96.     freqs = frequencies(n)
  97.  
  98.     # generate reconstructed wave that is the sum of all spectral components
  99.     r_wave = generate_wave(amps, freqs)
  100.  
  101.     # generate original square wave
  102.     s_wave = square_wave()
  103.  
  104.     # plot original square wave and reconstructed wave on same graph
  105.     plot_two_waves(r_wave, s_wave, n, filename)
  106.  
  107.     # plot spectrum on separate graph
  108.     plot_spectrum(n)
  109.  
  110.     # normalize the square wave
  111.     normalize(s_wave)
  112.  
  113.     # write the square wave to a sound file
  114.     write_sound(s_wave)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top