Advertisement
Guest User

Untitled

a guest
Apr 26th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. from scipy import signal
  2. from nmigen import *
  3. from nmigen.back.pysim import *
  4. from nmigen.cli import main
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7. import wave, struct
  8.  
  9. class Crossover(Elaboratable):
  10.     def __init__(self, cutoff, sampleRate = 48000):
  11.         self.input = Signal(32)
  12.         self.output = Signal(32)
  13.         self.sampleRate = sampleRate
  14.         self.cutoff = cutoff
  15.         (self.num, self.den) = signal.butter(2, [cutoff], btype='lowpass', analog=False, fs=sampleRate)
  16.  
  17.     def polynome(self):
  18.         return (self.num, self.den)
  19.  
  20.     def plot(self):
  21.         w, h = signal.freqz(self.num, self.den, fs=self.sampleRate)
  22.         plt.plot(w, 20 * np.log10(abs(h)))
  23.         plt.xscale('log')
  24.         plt.title('Butterworth filter frequency response')
  25.         plt.xlabel('Frequency [radians / second]')
  26.         plt.ylabel('Amplitude [dB]')
  27.         plt.margins(0, 0.1)
  28.         plt.grid(which='both', axis='both')
  29.         plt.axvline(self.cutoff, color='green') # cutoff frequency
  30.         plt.show()
  31.  
  32.     def elaborate(self, platform):
  33.         m = Module()
  34.  
  35.         x_sigs = []
  36.         for i in range(len(self.num)):
  37.             x_sigs.append(Signal(32))
  38.  
  39.         m.d.comb += x_sigs[0].eq(self.input)
  40.  
  41.         for i in range(1, len(self.num)):
  42.             m.d.sync += x_sigs[i].eq(x_sigs[i-1])
  43.  
  44.         y_sigs = []
  45.         for i in range(len(self.den)):
  46.             y_sigs.append(Signal(32))
  47.  
  48.         m.d.comb += y_sigs[0].eq(self.output)
  49.  
  50.         for i in range(1, len(self.den)):
  51.             m.d.sync += y_sigs[i].eq(y_sigs[i-1])
  52.  
  53.         outputSum = 0
  54.         for i in range(len(self.num)):
  55.             outputSum += x_sigs[i]*int(self.num[i]*256)
  56.         for i in range(1, len(self.den)):
  57.             outputSum -= y_sigs[i]*int(self.den[i]*256)
  58.  
  59.         buf = Signal(32)
  60.         m.d.comb += buf.eq(outputSum)
  61.  
  62.         m.d.comb += self.output.eq(outputSum >> 8)
  63.  
  64.         return m
  65.  
  66. if __name__ == "__main__":
  67.     crossover = Crossover(1000, 48000)
  68.     #crossover.plot()
  69.     #print(crossover.polynome())
  70.  
  71.     with Simulator(crossover) as sim:
  72.         def process():
  73.             audioIn = wave.open("input.wav", "r")
  74.             audioOut = wave.open("output.wav", "w")
  75.             audioOut.setnchannels(1)
  76.             audioOut.setsampwidth(2)
  77.             audioOut.setframerate(48000)
  78.             print("Frames ", audioIn.getnframes())
  79.             for i in range(audioIn.getnframes()):
  80.                 print("Sample ", i)
  81.                 sample = struct.unpack("<h", audioIn.readframes(1))
  82.                 yield crossover.input.eq(sample[0]+32768)
  83.                 yield Tick()
  84.                 #print((yield crossover.output)-32768)
  85.                 audioOut.writeframes(struct.pack("<h", (yield crossover.output)-32768))
  86.             audioOut.close()
  87.  
  88.         sim.add_clock(1/48000)
  89.         sim.add_process(process)
  90.         sim.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement