clear
close all
clc
%% -----滤波器设计---------------
%-----巴特沃斯滤波器设计---------------
Fs = 960; % Sampling Frequency
N = 8; % Order
Fc1 = 0.8225; % First Cutoff Frequency
Fc2 = 100; % Second Cutoff Frequency
h = fdesign.bandpass('N,F3dB1,F3dB2', N, Fc1, Fc2, Fs);
Hd1 = design(h, 'butter');
[b1,a1] = tf(Hd1); %得到传递函数
G1 = tf(b1,a1); %得到传递函数
[h,w] = freqz(b1,a1,'whole',2001);%求取系统频率响应
figure('NumberTitle','off', 'Name','Butterworth IIR filter')
subplot(2,1,1)
plot(w*Fs/(2*pi),20*log10(abs(h)),'LineWidth',4);
xlim([0 Fs/2]);
grid on
set(gca,'fontsize',20,'fontweight','bold')
title('Amplitude-Frequency response curve of Butterworth IIR filter','fontsize',20,'fontweight','bold')
xlabel('Frequency (Hz)','fontsize',20,'fontweight','bold')
ylabel('Magnitude (dB)','fontsize',20,'fontweight','bold')
subplot(2,1,2)
plot(w*Fs/(2*pi),180/pi*unwrap(angle(h)),'LineWidth',4);
xlim([0 Fs/2]);
grid on
set(gca,'fontsize',20,'fontweight','bold')
title('Phase-Frequency response curve of Butterworth IIR filter','fontsize',20,'fontweight','bold')
xlabel('Frequency (Hz)','fontsize',20,'fontweight','bold')
ylabel('Phase (degrees)','fontsize',20,'fontweight','bold')
% figure(1)
% subplot(211)
% plot(w/pi,20*log10(abs(h)))
% xlabel('Normalized Frequency (\times\pi rad/sample)')
% ylabel('Magnitude (dB)')
% subplot(212)
% plot(w/pi,angle(h))
% xlabel('Normalized Frequency (\times\pi rad/sample)')
% ylabel('degree (^o)')
% plot(w/pi,abs(X)); %画解卷绕后的幅值响应
% fvtool(Hd1,'Analysis','magnitude'); % Open FVTool with magnitude display
% fvtool(Hd1,'Analysis','phase');
%-----切比雪夫I型滤波器设计---------------
N = 5; % Order
Fpass1 = 0.8225; % First Passband Frequency
Fpass2 = 100; % Second Passband Frequency
Apass = 1; % Passband Ripple (dB)
h2 = fdesign.bandpass('n,fp1,fp2,ap', N+1, Fpass1, Fpass2, Apass, Fs);
Hd2 = design(h2, 'cheby1','SystemObject', true);
% fvtool(Hd2,'Analysis','magnitude'); % Open FVTool with magnitude display
% fvtool(Hd2,'Analysis','phase');
[b2,a2] = tf(Hd2); %得到传递函数
G2 = tf(b2,a2); %得到传递函数
[h,w] = freqz(b2,a2,'whole',2001);%求取系统频率响应
figure('NumberTitle','off', 'Name','chebyshev IIR filter')
subplot(2,1,1)
plot(w*Fs/(2*pi),20*log10(abs(h)),'LineWidth',4);
xlim([0 Fs/2]);
grid on
set(gca,'fontsize',20,'fontweight','bold')
title('Amplitude-Frequency response curve of Butterworth IIR filter','fontsize',20,'fontweight','bold')
xlabel('Frequency (Hz)','fontsize',20,'fontweight','bold')
ylabel('Magnitude (dB)','fontsize',20,'fontweight','bold')
subplot(2,1,2)
plot(w*Fs/(2*pi),180/pi*unwrap(angle(h)),'LineWidth',4);
xlim([0 Fs/2]);
grid on
set(gca,'fontsize',20,'fontweight','bold')
title('Phase-Frequency response curve of Butterworth IIR filter','fontsize',20,'fontweight','bold')
xlabel('Frequency (Hz)','fontsize',20,'fontweight','bold')
ylabel('Phase (degrees)','fontsize',20,'fontweight','bold')
%-----椭圆滤波器设计---------------
N = 4; % Order
Astop1 = 40; % First Stopband Attenuation (dB)
Astop2 = 40; % Second Stopband Attenuation (dB)
h3 = fdesign.bandpass('n,fp1,fp2,ast1,ap,ast2', N, Fpass1, Fpass2,Astop1, Apass, Astop2, Fs);
Hd3 = design(h3, 'ellip','SystemObject', true);
[b3,a3] = tf(Hd3); %得到传递函数
G3 = tf(b3,a3); %得到传递函数
% fvtool(Hd3,'Analysis','magnitude'); % Open FVTool with magnitude display
% fvtool(Hd3,'Analysis','phase');
[h,w] = freqz(b3,a3,'whole',2001);%求取系统频率响应
figure('NumberTitle','off', 'Name','elliptic IIR filter')
subplot(2,1,1)
plot(w*Fs/(2*pi),20*log10(abs(h)),'LineWidth',4);
xlim([0 Fs/2]);
grid on
set(gca,'fontsize',20,'fontweight','bold')
title('Amplitude-Frequency response curve of Butterworth IIR filter','fontsize',20,'fontweight','bold')
xlabel('Frequency (Hz)','fontsize',20,'fontweight','bold')
ylabel('Magnitude (dB)','fontsize',20,'fontweight','bold')
subplot(2,1,2)
plot(w*Fs/(2*pi),180/pi*unwrap(angle(h)),'LineWidth',4);
xlim([0 Fs/2]);
grid on
set(gca,'fontsize',20,'fontweight','bold')
title('Phase-Frequency response curve of Butterworth IIR filter','fontsize',20,'fontweight','bold')
xlabel('Frequency (Hz)','fontsize',20,'fontweight','bold')
ylabel('Phase (degrees)','fontsize',20,'fontweight','bold')
%% -----滤波器设计---------------
% -----滤波1---------------
load signal1.mat
load time.mat
signal1 = Signal_VCG_Breath_Left_Thorax;clear Signal_VCG_Breath_Left_Thorax
t = Time_VCG_Breath_Left_Thorax(:,1); clear Time_VCG_Breath_Left_Thorax
s1 = filter(b1,a1,signal1(:,1));
fs = 960;
l = length(s1);
Y11 = fft(signal1(:,1),l);Y12 = fft(s1,l);
Pyy11 = Y11.*conj(Y11)/l;Pyy12 = Y12.*conj(Y12)/l;
f = 1000/l*(0:floor(l/2)); f = f';
figure(1)
plot(f,Pyy11(1:floor(l/2)+1),'b',f,Pyy12(1:floor(l/2)+1),'r')
xlim([0 2.5])
legend('before filter','after filter')
figure(2)
plot(f,Pyy11(1:floor(l/2)+1),'b',f,Pyy12(1:floor(l/2)+1),'r')
xlim([90 110])
legend('before filter','after filter')
figure(3)
plot(t,signal1(:,1),'b',t,s1,'r')
xlim([4.5 7.5])
legend('before filter','after filter')
% -----滤波2---------------
s2 = filter(b2,a2,signal1(:,1));
Y21 = fft(signal1(:,1),l);Y22 = fft(s2,l);
Pyy21 = Y21.*conj(Y21)/l;Pyy22 = Y22.*conj(Y22)/l;
figure(21)
plot(f,Pyy21(1:floor(l/2)+1),'b',f,Pyy22(1:floor(l/2)+1),'r')
xlim([0 2.5])
legend('before filter','after filter')
figure(22)
plot(f,Pyy21(1:floor(l/2)+1),'b',f,Pyy22(1:floor(l/2)+1),'r')
xlim([90 110])
legend('before filter','after filter')
figure(23)
plot(t,signal1(:,1),'b',t,s2,'r')
xlim([4.5 7.5])
legend('before filter','after filter')
% -----滤波3---------------
s3 = filter(b3,a3,signal1(:,1));
Y31 = fft(signal1(:,1),l);Y32 = fft(s3,l);
Pyy31 = Y31.*conj(Y31)/l;Pyy32 = Y32.*conj(Y32)/l;
figure(31)
plot(f,Pyy31(1:floor(l/2)+1),'b',f,Pyy32(1:floor(l/2)+1),'r')
xlim([0 2.5])
legend('before filter','after filter')
figure(32)
plot(f,Pyy31(1:floor(l/2)+1),'b',f,Pyy32(1:floor(l/2)+1),'r')
xlim([90 110])
legend('before filter','after filter')
figure(33)
plot(t,signal1(:,1),'b',t,s3,'r')
xlim([4.5 7.5])
legend('before filter','after filter')