ImaGIN_bandpass.m
8.82 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
function xf = ImaGIN_bandpass(x,Fs,Fp1,Fp2)
% Bandpass filter for the signal x. An acausal fft
% algorithm is applied (i.e. no phase shift). The filter functions is
% constructed from a Hamming window (default window used in "fir2" Matlab function).
% to avoid ripples in the frequency reponse (windowing is a smoothing in frequency domain)
%
% Fs : sampling frequency
%
% The passbands (Fp1 Fp2) frequencies are defined in Hz as
% ----------
% /| | \
% / | | \
% / | | \
% / | | \
% ---------- | | -----------------
% | |
% Fs1 Fp1 Fp2 Fs2
%
% DEFAULTS values
% Fs1 = Fp1 - 0.5 in Hz
% Fs2 = Fp2 + 0.5 in Hz
%
%
% If NO OUTPUTS arguments are assigned the filter function H(f) and
% impulse response are plotted.
%
% NOTE: for long data traces the filter is very slow.
%
% EXEMPLE
% x= sin(2*pi*12*[0:1/200:10])+sin(2*pi*30*[0:1/200:10])
% y=bandpassFilter(x,200,5,20); bandpass filter between 5 and 20 Hz
%------------------------------------------------------------------------
% Originally produced by the Helsinki University of Technology,
% Adapted by Mariecito SCHMUCKEN 2001
%------------------------------------------------------------------------
%Default values in Hz
Fs1 = Fp1 - 0.5;
Fs2 = Fp2 + 0.5;
if size(x,1) == 1
x = x';
end
% Make x EVEN
Norig = size(x,1);
if rem(Norig,2)
x = [x' zeros(size(x,2),1)]';
end
% Normalize frequencies
Ns1 = Fs1/(Fs/2);
Ns2 = Fs2/(Fs/2);
Np1 = Fp1/(Fs/2);
Np2 = Fp2/(Fs/2);
% Construct the filter function H(f)
N = size(x,1);
Nh = N/2;
B = fir2(N-1,[0 Ns1 Np1 Np2 Ns2 1],[0 0 1 1 0 0]);
% Make zero-phase filter function
H = abs(fft(B));
IPR = real(ifft(H));
if size(x,2) > 1
for k=1:size(x,2)
xf(:,k) = real(ifft(fft(x(:,k)) .* H'));
end
xf = xf(1:Norig,:);
else
xf = real(ifft(fft(x') .* H));
xf = xf(1:Norig);
end
x=x(1:Norig);
% if NO OUTPUT argument then plots
if nargout == 0
f = Fs*(0:Nh-1)/(N);
freqz(IPR,1,f,Fs);
figure, subplot(2,1,1)
plot(f,H(1:Nh));
xlim([0 2*Fs2])
title('Filter function H(f)')
xlabel('Frequency (Hz)')
subplot(2,1,2)
plot((1:Nh)/Fs,IPR(1:Nh))
xlim([0 2/Fp1])
xlabel('Time (sec)')
ylim([min(IPR) max(IPR)])
title('Impulse response')
figure, subplot(211),
periodogram(x,hamming(Norig),1024,200);
subplot(212), periodogram(xf,hamming(Norig),1024,200);
end
end
% Copyright (C) 2000 Paul Kienzle
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
% usage: b = fir2(n, f, m [, grid_n [, ramp_n]] [, window])
%
% Produce an FIR filter of order n with arbitrary frequency response,
% returning the n+1 filter coefficients in b.
%
% n: order of the filter (1 less than the length of the filter)
% f: frequency at band edges
% f is a vector of nondecreasing elements in [0,1]
% the first element must be 0 and the last element must be 1
% if elements are identical, it indicates a jump in freq. response
% m: magnitude at band edges
% m is a vector of length(f)
% grid_n: length of ideal frequency response function
% defaults to 512, should be a power of 2 bigger than n
% ramp_n: transition width for jumps in filter response
% defaults to grid_n/20; a wider ramp gives wider transitions
% but has better stopband characteristics.
% window: smoothing window
% defaults to hamming(n+1) row vector
% returned filter is the same shape as the smoothing window
%
% To apply the filter, use the return vector b:
% y=filter(b,1,x);
% Note that plot(f,m) shows target response.
%
% Example:
% f=[0, 0.3, 0.3, 0.6, 0.6, 1]; m=[0, 0, 1, 1/2, 0, 0];
% [h, w] = freqz(fir2(100,f,m));
% plot(f,m,';target response;',w/pi,abs(h),';filter response;');
% Feb 27, 2000 PAK
% use ramping on any transition less than ramp_n units
% use 2^nextpow2(n+1) for expanded grid size if grid is too small
% 2001-01-30 PAK
% set default ramp length to grid_n/20 (i.e., pi/20 radians)
% use interp1 to interpolate the grid points
% better(?) handling of 0 and pi frequency points.
% added some demos
function b = fir2(n, f, m, grid_n, ramp_n, window)
if nargin < 3 || nargin > 6
disp('b = fir2(n, f, m [, grid_n [, ramp_n]] [, window])');
return
end
% verify frequency and magnitude vectors are reasonable
t = length(f);
if t<2 || f(1)~=0 || f(t)~=1 || any(diff(f)<0)
disp('frequency must be nondecreasing starting from 0 and ending at 1');
return
end
if t ~= length(m)
disp('frequency and magnitude vectors must be the same length');
return
end
% find the grid spacing and ramp width
if (nargin>4 && length(grid_n)>1) || (nargin>5 && (length(grid_n)>1 || length(ramp_n)>1))
disp('grid_n and ramp_n must be integers');
return
end
if nargin < 4, grid_n=512; end
if nargin < 5, ramp_n=grid_n/20; end
% find the window parameter, or default to hamming
w=[];
if length(grid_n)>1, w=grid_n; grid_n=512; end
if length(ramp_n)>1, w=ramp_n; ramp_n=grid_n/20; end
if nargin < 6, window=w; end
if isempty(window), window=hamming(n+1); end
if ~isreal(window), window=feval(window, n+1); end
if length(window) ~= n+1, disp('window must be of length n+1'); return;end
% make sure grid is big enough for the window
if 2*grid_n < n+1, grid_n = 2^nextpow2(n+1); end
% Apply ramps to discontinuities
if (ramp_n > 0)
% remember original frequency points prior to applying ramps
basef = f; basem = m;
% separate identical frequencies
idx = find (diff(f) == 0);
f(idx) = f(idx) - ramp_n/grid_n/2;
f(idx+1) = f(idx+1) + ramp_n/grid_n/2;
% make sure the grid points stay monotonic
idx = find (diff(f) < 0);
f(idx) = (basef(idx) + basef(idx+1))/2;
f(idx+1) = (basef(idx) + basef(idx+1))/2;
% preserve window shape even though f may have changed
m = interp1(basef, basem, f);
% plot(f,m,';ramped;',basef,basem,';original;'); pause;
end
% interpolate between grid points
grid = interp1(f,m,linspace(0,1,grid_n+1)');
% Transform frequency response into time response and
% center the response about n/2, truncating the excess
b = ifft([grid ; grid(grid_n:-1:2)]);
mid = (n+1)/2;
b = real ([ b((2*grid_n-floor(mid)+1) : (2*grid_n)) ; b(1:ceil(mid)) ]);
% Multiplication in the time domain is convolution in frequency,
% so multiply by our window now to smooth the frequency response.
if size(window,1) > 1
b = b .* window;
else
b = b' .* window;
end
b=b';
end
% Copyright (C) 1995, 1996, 1997 Andreas Weingessel
%
% This file is part of Octave.
%
% Octave is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2, or (at your option)
% any later version.
%
% Octave is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Octave; see the file COPYING. If not, write to the Free
% Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
% 02110-1301, USA.
% -*- texinfo -*-
% @deftypefn {Function File} {} hamming (@var{m})
% Return the filter coefficients of a Hamming window of length @var{m}.
%
% For a definition of the Hamming window, see e.g. A. V. Oppenheim &
% R. W. Schafer, "Discrete-Time Signal Processing".
% @end deftypefn
% Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
% Description: Coefficients of the Hamming window
function c = hamming (m)
if (nargin ~= 1)
disp ('hamming (m)');
return
end
if (~ (length(m)==1 && (m == round(m)) && (m > 0)))
error ('hamming: m has to be an integer > 0');
end
if (m == 1)
c = 1;
else
m = m - 1;
c = 0.54 - 0.46 * cos (2 * pi * (0:m)' / m);
end
end