matlab 生成滤波器系数

本文借鉴 小墨学FPGA 的文章 《零基础学FPGA( 三十二) 写在京城,多级FIR半带滤波器的FPGA实现 》

地址是: http://bbs.elecfans.com/forum.php?mod=viewthread&tid=509022

假设我们的合成信号是由100,300和600hz的正弦波相加而成的信号,并将其进行8bit量化,初始采样频率为6400hz,现在我需要将采样信号降为800hz,并要求获得100hz有用信号,要求滤波器的通阻带容限为0.1,设计这样一个FIR半带滤波器。

clear all
clc
close all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 通带截止频率近似3KHz,阻带截止频率近似5KHz
fp = 100; % 要求最后一级滤波器的通带截止频率
fc = 300; % 要求最后一级滤波器的阻带截止频率
fs = 6400; %原始采样频率
fs_out = 800; % 要求降频后的采样频率
dev=0.1; %要求的滤波器通阻带容限
D=fs/fs_out; %求总抽取倍数
N=log2(D) ; %求滤波器级数
devi=dev/N; % 求每级滤波器通阻带容限

% 设计前2级半带滤波器
hin1 = firhalfband('minorder',fp/(fs/2),dev); %designs a lowpass minimum-order filter,
% with passband edge fp. The peak ripple is constrained by the scalar dev.
% This design uses the equiripple method.

hin2 = firhalfband('minorder',fp/(fs/4),dev);
fid=fopen('E:\WORK\DRS6000_Q\MATLAB CODE\hin1.txt','wt');
fprintf(fid, '%d\n', hin1);
fclose(fid);

fid=fopen('E:\WORK\DRS6000_Q\MATLAB CODE\hin2.txt','wt');
fprintf(fid, '%d\n', hin2);
fclose(fid);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 设计最后一级最优低通滤波器
f = [fp,fc]; % 最优低通滤波器带宽
a = [1 0]; % 最优低通滤波器幅频特性,低通滤波器
devk = [devi,dev] ; % 要求的滤波器通阻带容限
mag = [1 1 0 0]; %最优滤波器幅度特性
fpm = [0 fp/((fs_out*2)/2) fc/((fs_out*2)/2) 1];
[n,wn,beta,firtype] = kaiserord(f,a,devk,fs_out*2); %调用函数求滤波器最小阶数
hin3 =firpm(n,fpm,mag); %调用函数设计最优滤波器
% freqz(hin3);


fid=fopen('E:\WORK\DRS6000_Q\MATLAB CODE\hin3.txt','wt');
fprintf(fid, '%d\n', hin3);
fclose(fid);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

f1 = 100; % 信号频率
f2 = 300; % 信号频率
f3 = 600; % 信号频率
lengthy = 1024;
n = 0:1:lengthy-1;
t = n/fs;
si = sin(2*pi*f1*t)+sin(2*pi*f2*t)+sin(2*pi*f3*t);
figure
plot(t,si);
freq=linspace(-fs/2,fs/2,lengthy);
figure
plot(freq,fftshift( abs(fft(si)) ));
xlim( [ 0, 1000 ] ); %设置横轴范围0~1000
xlabel('Frequency ');
title('Magnitude spectrum of chirp signal');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%滤波与抽取过程
F1_out = filter (hin1,1,si); %第一级半带滤波器
s1 = F1_out (1:2:length(F1_out)); %2倍抽取
F1_max = max (abs(F1_out));

F2_out = filter (hin2,1,s1); %第二级半带滤波器
s2 = F2_out (1:2:length(F2_out)); %2倍抽取

F3_out = filter (hin3,1,s2); %第二级半带滤波器
sout = F3_out (1:2:length(F3_out)); %2倍抽取
sout = sout/max(sout);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
lengths = lengthy/8;
n1 = 0:1:lengths-1;
t1=n1/fs;
figure
plot(t1,sout);
freq=linspace(-fs/16,fs/16,lengths);
figure
plot(freq,fftshift( abs(fft(sout)) ));
xlim( [ 0, 1000 ] ); %设置横轴范围0~1000
xlabel('Frequency ');
title('Magnitude spectrum of chirp signal');
figure
subplot(211)
stem(si);
subplot(212)
stem(sout);

% % 前2级半带滤波器的幅频相应
% figure
% impz(hin1);
% figure
% impz(hin2);
%
% h1 = mfilt.firdecim(2,hin1); % Create a polyphase decimator
% h2 = mfilt.firdecim(2,hin2); % Create a polyphase decimator
% freqz(h1);
% freqz(h2);

转载于:https://www.cnblogs.com/chuanchuan304/p/5888790.html

你可能感兴趣的:(matlab 生成滤波器系数)