Design and Simulation of FIR Filter Based on Quartus II and MATLAB (1)

        Keywords: Quartus II MATLAB FIR filter IP core

        The entire design and simulation process is roughly divided into four steps: the first step is to design filter coefficients in MATLAB and generate mixed frequency sinusoidal signals as simulation input for functional simulation; the second step is to call the IP core in Quartus II to import coefficients to generate filters; third The first step is to call the ROM IP core in Quartus II to store the simulation input as a sine signal generator for simulation; the fourth step is to export the Quartus II simulation output to MATLAB for drawing and comparison with the input.

1 MATLAB design of FIR filter coefficients and FIR filter simulation
    1.1 Design of FIR filter coefficients

        There are two ways to design filter coefficients in MATLAB: Method 1 can call the graphical tool FDATOOL: enter fdatool on the command line --> set the filter pass-stop band, sampling frequency, select the design method --> generate filter coefficients, export Go to Workspace --> Quantize Filter Coefficients and save it to a text file. There are many tutorials on the Internet for specific settings, so I won't go into details.


Method 2 is to design the filter coefficients by typing the code:

fs=8000; % sampling frequency
fp=1500; % passband maximum frequency
fstop=2000; % stopband minimum frequency
wp=2*pi*fp/fs;
ws=2*pi*fstop/fs;
B=ws-wp ;% transition band
n=ceil(8*pi/B);% transition band width of Hanning window is 8*pi/n; n-order filter; ceil() function means rounding up
wn=(wp+B/ 2)/pi;%wn is the normalized cut-off frequency
w=hann(n);%w is the window function type, Hanning window
b=fir1(n-1,wn,w);%Call the fir1 function to achieve n order fir filter
[hw,ww]=freqz(b,1,512,fs); %b is the numerator of the tap coefficient, 1 is the denominator of the tap coefficient, 512 means that 512 points fft is converted from the time domain to the frequency domain, hw is the frequency Response, ww is the frequency vector
% draw the actual impulse response and frequency response graph
figure
m=0:n-1;
subplot(311);
stem(m,b);grid on;
xlabel('n');ylabel(' h(n)');
title('Actual impulse response');
subplot(312)
plot(ww,20*log10(abs(hw)));grid on;
xlabel('frequency(Hz)');ylabel ('Magnitude(dB)');
title('Amplitude-frequency characteristic');
subplot(313)
plot(ww,angle(hw));grid on;
xlabel('frequency(Hz)');ylabel('Phase');
title('phase frequency characteristic');
% filter coefficient for quantization
b12= round(b/max(abs(b))*(2^11-1));%12bit quantization
b14=round(b/max(abs(b))*(2^13-1));%14bit quantization
% Write the generated filter coefficient data to the file required by the FPGA
fid=fopen('D:\Altera_pro\lp_fir\b.txt','w');
fprintf(fid,'%8d\r\n' ,b);
fclose(fid);
fid1=fopen('D:\Altera_pro\lp_fir\b12.txt','w');
fprintf(fid1,'%d\r\n',b12);
fclose(fid1 );
fid2=fopen('D:\Altera_pro\lp_fir\b14.txt','w');
fprintf(fid2,'%8d\r\n',b14);
fclose(fid2);


Here I quantized the filter coefficients with 12-bit and 14-bit respectively, and then I chose 12-bit quantization; the reason for quantization is because the FIR filter is a digital filter, so both the coefficients and the input sinusoidal signal are quantized into a digital signal.

    1.2 Implementation and simulation of filter in MATLAB 

        1.2.1 Generation of mixed frequency sine signal (here, we must pay attention to the sampling frequency when setting the sampling frequency must be consistent with the sampling frequency when designing the filter coefficients)

% Generate mixed frequency input waveform
f1 = 1000;
f2 = 2500;
t = 0:1/fs:0.01-1/fs;
x1 = 1 + sin(2*pi*f1*t);
x2 = 1 + sin(2 *pi*f2*t);
x = x1 + x2;
figure;
subplot(411);
plot(t,x1);
title('1000Hz sinusoidal signal');
subplot(412);
plot(t,x2);
title ('2500Hz sine signal');
subplot(413);
plot (t,x);
title('Mixed frequency sine signal');
x12=round(x/max(abs(x))*(2^11-1 ));%12bit quantized
subplot(414);
plot(t,x12);
title('quantized sinusoidal signal waveform');
X1=fft(x1);
X2=fft(x2);
X=fft(x);
X12=fft(x12);
figure;
subplot(411);
stem(abs(X1));
title('1000Hz sinusoidal signal spectrum');
subplot(412);
stem(abs(X2));
title('2500Hz sinusoidal signal spectrum');
subplot(413);
stem (abs(X));
title('mixed frequency sinusoidal signal spectrum');
x12=round (x/max(abs(x))*(2^11-1));%12bit quantized
subplot(414);
stem(abs(X12));
title('quantized sinusoidal signal spectrum');

        Also, since the FIR filter is a digital filter, we need to do 12-bit quantization of the simulated input signal.



The frequency represented by each point on the abscissa in Figure 4 is 100Hz, and it can be seen that the input signal contains two frequencies of 1000Hz and 2500Hz.  

        1.2.2 Implementation and Simulation of Filters

        The realization and simulation of the filter in MATLAB can be realized by calling the filter function in addition to being realized in simulink.

% Filter implementation
y=filter(b12,1,x12);
figure;
subplot(311);
plot(x);
title('Waveform before filtering');
subplot(312);
plot(x1);
title('1000Hz Sine signal');
subplot(313);
plot(y);
title('Filtered waveform');

%spectral analysis
X1=fft(x1);
X=fft(x);
Y=fft(y);
figure;
subplot(311);
stem(abs(X1));
title('1000Hz sinusoidal signal spectrum');
subplot(312);
stem(abs(X));
title('Spectrum before filtering');
subplot(313);
stem(abs(Y));
title('Filtered spectrum'); 

It can be seen from Figure 6 that the filter output is basically zero when the first 32 data are input. This is because the sampled data has not been put into the multiply-add unit at this stage, so the output y(n) is basically zero. It can be seen from Figure 6 that the
filter successfully filters out the signal component with a frequency of 2500Hz, but there is still noise around 1000Hz, so it is not an ideal low-pass filter.


        Let’s write this first! The next article will share how to implement filter design and simulation in Quartus II!


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324515075&siteId=291194637