0% found this document useful (0 votes)
10 views3 pages

Fir

This document implements a synthesizable FIR filter using Migen and tests its functionality with sine wave and impulse response inputs. It computes filter coefficients using SciPy and visualizes the input and output signals, as well as the frequency response of the filter. Additionally, it generates and saves the Verilog source code for the FIR filter.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Fir

This document implements a synthesizable FIR filter using Migen and tests its functionality with sine wave and impulse response inputs. It computes filter coefficients using SciPy and visualizes the input and output signals, as well as the frequency response of the filter. Additionally, it generates and saves the Verilog source code for the FIR filter.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

from functools import reduce

from operator import add

from math import cos, pi


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

from migen import *


from migen.fhdl import verilog

# A synthesizable FIR filter.


class FIR(Module):
def __init__(self, coef, wsize=16):
self.coef = coef
self.wsize = wsize
self.i = Signal((self.wsize, True))
self.o = Signal((self.wsize, True))

###

muls = []
src = self.i
for c in self.coef:
sreg = Signal((self.wsize, True))
self.sync += sreg.eq(src)
src = sreg
c_fp = int(c*2**(self.wsize - 1))
muls.append(c_fp*sreg)
sum_full = Signal((2*self.wsize-1, True))
self.sync += sum_full.eq(reduce(add, muls))
self.comb += self.o.eq(sum_full >> self.wsize-1)

# A test bench for our FIR filter.


# Generates a sine wave at the input and records the output.
def fir_tb(dut, frequency, inputs, outputs):
f = 2**(dut.wsize - 1)
print("printando f",f)
for cycle in range(200):
v = 0.1*cos(2*pi*frequency*cycle)
yield dut.i.eq(int(v*f))
fv=(int(v*f))
entrada.append(fv)
inputs.append(v)
outputs.append((yield dut.o)/f)
yield

def impulse_response_tb(dut, inputs, outputs):


f = 2*(dut.wsize - 1)
impulse = [1] + ([0]*36) # impulso seguido de zeros
for v in impulse:
yield dut.i.eq(v*f)
inputs.append(v)
outputs.append((yield dut.o)/f)
yield

if __name__ == "__main__":
# Compute filter coefficients with SciPy.]
cutoff=800.0
trans_width=100
fs=2000
numtaps=30
coef = signal.remez(numtaps, [0, cutoff, cutoff+trans_width, 0.5*fs],
[1,0],fs=fs)

in_signals = []
out_signals = []
entrada = []
for frequency in [0.025,0.1,0.45,0.5]:
dut = FIR(coef)
tb = fir_tb(dut, frequency, in_signals,out_signals)
run_simulation(dut, tb)
with open("entrada_default.txt", "w") as file:
for value in in_signals:
file.write(f"{value}\n")

with open("entrada.txt", "w") as file:


for value in entrada:
file.write(f"{value}\n")
with open("saida.txt", "w") as file:
for value in out_signals:
file.write(f"{value}\n")

plt.plot(in_signals)
plt.plot(out_signals)
plt.title("Respostas para 50hz 200hz 900hz 1000hz")
plt.xlabel("Frequencia:50hz 200hz 900hz 1000hz" )
plt.ylabel("Amplitude")
plt.grid()
plt.show()

impulse_in = []
impulse_out = []
dut = FIR(coef)
run_simulation(dut, impulse_response_tb(dut, impulse_in, impulse_out))

plt.figure()
plt.title("Resposta ao Impulso")
plt.stem(impulse_out, use_line_collection=True)
plt.xlabel("Amostra")
plt.ylabel("Amplitude")
plt.grid()
plt.show()

# Plot data from the input and output waveforms.


w, h = signal.freqz(coef, worN=8000, fs=fs)
plt.figure()
plt.plot(w, 20 * np.log10(abs(h)))
plt.title("Resposta em Frequência do Filtro FIR")
plt.xlabel("Frequência (Hz)")
plt.ylabel("Magnitude (dB)")
plt.grid()
plt.xlim(0, 1000)
plt.show()
# Print the Verilog source for the filter.

fir = FIR(coef)
print(verilog.convert(fir, ios={fir.i, fir.o}).write("fir.sv"))

You might also like