-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplefilter.m
More file actions
146 lines (101 loc) · 2.58 KB
/
Copy pathsimplefilter.m
File metadata and controls
146 lines (101 loc) · 2.58 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
function outsig = simplefilter(insig,fs,downfactor,highpass,lowpass,notch,gain)
% simplefilter
% Version: 1.00
%
% Description:
% ------------
% This function filter a given signal simply.
%
% Input:
% ------
% insig - vector/matrix of signal
%
% fs - sampling frequency
%
% downfactor - downsampling factor (1 for no downsampling)
%
% highpass - pass higher frequencies than highpass in hz (-inf for no
% highpass)
%
% lowpass - pass lower frequencies than lowpass in hz (inf for no lowpass)
%
% notch - notch frequency to remove ([] for no notch)
%
% gain - gain factor (scalar multiply 1 for nothing)
%
% Output:
% -------
% outsig - the processed signal
% Order of prcoessing:
% --------------------
% 1. High pass
% 2. Notch
% 3. Downsampling
% 4. Low pass
% 5. Gain
% Check arguments
if (isempty(insig))
error('Empty input signal...');
end
if (fs<=0)
error('Errornous sampling frequency');
end
if (downfactor<1)
error('Downsampling factor must be 1 or higher');
end
if (highpass>lowpass)
error('Lowpass frequency cutoff must be higher than highpass');
end
% Init
nyquist = fs/2; % Nyquist frequency
pass_order = 2; % Low pass and high pass order
notch_order = 2; % Notch order
notchmargin = 1; % Margin of notch
nrows = size(insig,1);
ncols = size(insig,2);
% 1. High pass
if (highpass~=-inf)
[z,p,k] = butter(pass_order,(highpass/nyquist),'high');
[sos,g] = zp2sos(z,p,k); % Convert to SOS form
Hd = dfilt.df2tsos(sos,g);
for i=1:ncols
insig(:,i) = filter(Hd,insig(:,i));
end
end
% 2. Notch
if (~isempty(notch))
[b,a] = butter(notch_order,([notch-notchmargin notch+notchmargin]./nyquist),'stop');
for i=1:ncols
cwd = pwd;
cd('C:\Program Files\MATLAB\R2013a\toolbox\signal\signal');
insig(:,i) = filtfilt(b,a,insig(:,i));
cd(cwd);
end
end
% 3. Downsampling
if (downfactor~=1)
for i=1:ncols
if (i==1)
tempsig = downsample(insig(:,i),downfactor);
else
tempsig(:,i) = downsample(insig(:,i),downfactor);
end
end
insig = tempsig;
end
% 4. Low pass
if (lowpass~=inf)
[z,p,k] = butter(pass_order,(lowpass/nyquist),'low');
[sos,g] = zp2sos(z,p,k); % Convert to SOS form
Hd = dfilt.df2tsos(sos,g);
for i=1:ncols
insig(:,i) = filter(Hd,insig(:,i));
end
end
% 5. Gain
if (gain~=1)
insig = insig.*gain;
end
% Return the modificated signal
outsig = insig;
end