-
Notifications
You must be signed in to change notification settings - Fork 2
/
Butfilter.m
96 lines (73 loc) · 2.37 KB
/
Butfilter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
% Function name....: Butfilter
% Date.............: November 12, 2003
% Author...........: Adriano de Oliveira Andrade
% Description......:
% Low pass Butterworth filter
%
% Parameters.......:
% signal .............: input signal
% fsr.................: sampling frequency
% fc..................: cut-off frequency
% n...................: order
% Return...........:
% outSignal...........: filtered signal
% ybf.................: FFT before filtering
% y...................: FFT after filtering
function [filterCoef,f,outSignal]= Butfilter (signal,fsr,fc,n,filterType)
filterCoef = [];
y = [];
ybf = [];
outSignal= [];
nrAmos = length(signal);
% h = waitbar(0,'Please wait...');
% waitbar(n/nrAmos);
FreqRes = fsr/nrAmos; %frequency resolution (Hz);
NsamplesFilter = (nrAmos-1)/2;
%Estimating filter coefficients
% switch filterType,
% case 1, %low-pass filter
for i=1:(nrAmos-1)/2,
% waitbar(f/(nrAmos/2));%updating waitbar
% f and fsr (Hz)
f(i) = i*FreqRes;
w = (2*pi*f(i))/fsr; %(rad/s)
filterCoef(i) = sqrt(1/(1+(((1/(tan(pi*fc/fsr)))*tan(w/2))^(2*n))));
end;%for
% case 2,
% end;
% case 1: //passa alta
% for(f=0;f<nrAmos;f++)
% {
% //Calculo da frequencia w (rad/s)
% //f e fsr (Hz)
% w = (2*pi*f)/fsr;
% filterCoef[f] = sqrt(1/(1+pow(((1/(1/tan(pi*fh/fsr)))*1/tan(w/2)),(2*N))));
% }//for f*/
% break;
% case 2: //rejeita faixa
% for(f=0;f<nrAmos;f++)
% {
% double aux1, aux2;
% //Calculo da frequencia w (rad/s)
% //f e fsr (Hz)
% w = (2*pi*f)/fsr;
% aux1 = sqrt(1/(1+pow(((1/(tan(pi*fl/fsr)))*tan(w/2)),(2*N))));
% aux2=sqrt(1/(1+pow(((1/(1/tan(pi*fh/fsr)))*1/tan(w/2)),(2*N))));
% if(aux2>aux1) filterCoef[f] = aux2;
% else filterCoef[f] = aux1;
% }//for f
% % %Fast Fourier Transform of the input signal
y = fft(signal);
ybf=y;
k=1;
for i=2:(nrAmos-1)/2,
y(i) = y(i)*filterCoef(k);
k=k+1;
end;
k=1;
for i=nrAmos:-1:(nrAmos-1)/2+2,
y(i) = y(i)*filterCoef(k);
k = k+1;
end;
outSignal = real(ifft(y));
% close(h);%closing waiting bar