-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbutterworth_check.m
65 lines (51 loc) · 1.68 KB
/
butterworth_check.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
[vel, dt, npts, rsn] = readPEER(pwd,'/el_centro/RSN179_IMPVALL.H_H-E04140.VT2');
fcutlow = 1/3;
fcuthigh = 1/(0.2);
nOrder = 4;
vFil = bandpass_filter(vel,dt,fcutlow,fcuthigh,nOrder)';
tseries = 0:dt:(dt*npts-dt);
figure
plot(tseries, vel)
hold on
plot(tseries, vFil)
grid on
function [filtered_signal]= bandpass_filter(signal,dt,fcutlow,fcuthigh,nOrder)
% This function applies a band-pass filter to a signal,which contains a
% low-pass filter and a high-pass filter in the frequency domain.
% Input Variables
% signal : input time series
% dt : time step of the signal
% fcutlow : lower cutoff frequency (Hz)
% fcuthigh : upper cutoff frequency (Hz)
% nOrder : order of the filter
% Output Variables
% filtered_signal : bandpass-filtered time series
signal=reshape(signal,1,[]);
signal=signal(all(~isnan(signal),2),:);
for m=1:length(fcuthigh)
N = 2^nextpow2(length(signal));
Y = fft(signal,N);
df=1/(N*dt);
% lowpass filter
freq=0:df:N/2*df-df;
ff=freq./fcuthigh(m);
for i=2:length(ff)
% butterworth
H(i)= 1./sqrt(((1+ff(i).^(2*nOrder))));
Y(i)= Y(i).*H(i);
Y(length(Y)-i+2)=Y(length(Y)-i+2).*H(i);
end
% highpass filter
freq=0:df:N/2*df-df;
ff=freq./fcutlow(m);
H=zeros(1,length(N));
for i=2:length(ff)
H(i)= sqrt((ff(i).^(2*nOrder)))./sqrt(((1+ff(i).^(2*nOrder))));
Y(i)= Y(i).*H(i);
Y(length(Y)-i+2)=Y(length(Y)-i+2).*H(i);
end
Y1= (real(ifft(Y,N)))';
Y1=Y1(1:length(signal));
filtered_signal(m,:)=Y1;
end
end