]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/fir2.m
update packages
[CreaPhase.git] / octave_packages / signal-1.1.3 / fir2.m
1 ## Copyright (C) 2000 Paul Kienzle <pkienzle@users.sf.net>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## usage: b = fir2(n, f, m [, grid_n [, ramp_n]] [, window])
17 ##
18 ## Produce an FIR filter of order n with arbitrary frequency response,
19 ## returning the n+1 filter coefficients in b.
20 ##
21 ## n: order of the filter (1 less than the length of the filter)
22 ## f: frequency at band edges
23 ##    f is a vector of nondecreasing elements in [0,1]
24 ##    the first element must be 0 and the last element must be 1
25 ##    if elements are identical, it indicates a jump in freq. response
26 ## m: magnitude at band edges
27 ##    m is a vector of length(f)
28 ## grid_n: length of ideal frequency response function
29 ##    defaults to 512, should be a power of 2 bigger than n
30 ## ramp_n: transition width for jumps in filter response
31 ##    defaults to grid_n/20; a wider ramp gives wider transitions
32 ##    but has better stopband characteristics.
33 ## window: smoothing window
34 ##    defaults to hamming(n+1) row vector
35 ##    returned filter is the same shape as the smoothing window
36 ##
37 ## To apply the filter, use the return vector b:
38 ##       y=filter(b,1,x);
39 ## Note that plot(f,m) shows target response.
40 ##
41 ## Example:
42 ##   f=[0, 0.3, 0.3, 0.6, 0.6, 1]; m=[0, 0, 1, 1/2, 0, 0];
43 ##   [h, w] = freqz(fir2(100,f,m));
44 ##   plot(f,m,';target response;',w/pi,abs(h),';filter response;');
45
46 function b = fir2(n, f, m, grid_n, ramp_n, window)
47
48   if nargin < 3 || nargin > 6
49     print_usage;
50   endif
51
52   ## verify frequency and magnitude vectors are reasonable
53   t = length(f);
54   if t<2 || f(1)!=0 || f(t)!=1 || any(diff(f)<0)
55     usage("frequency must be nondecreasing starting from 0 and ending at 1");
56   endif
57   if t != length(m)
58     usage("frequency and magnitude vectors must be the same length");
59   endif
60
61   ## find the grid spacing and ramp width
62   if (nargin>4 && length(grid_n)>1) || (nargin>5 && (length(grid_n)>1 || length(ramp_n)>1))
63     usage("grid_n and ramp_n must be integers");
64   endif
65   if nargin < 4, grid_n=512; endif
66   if nargin < 5, ramp_n=grid_n/20; endif
67
68   ## find the window parameter, or default to hamming
69   w=[];
70   if length(grid_n)>1, w=grid_n; grid_n=512; endif
71   if length(ramp_n)>1, w=ramp_n; ramp_n=grid_n/20; endif
72   if nargin < 6, window=w; endif
73   if isempty(window), window=hamming(n+1); endif
74   if !isreal(window) || ischar(window), window=feval(window, n+1); endif
75   if length(window) != n+1, usage("window must be of length n+1"); endif
76
77   ## make sure grid is big enough for the window
78   if 2*grid_n < n+1, grid_n = 2^nextpow2(n+1); endif
79
80   ## Apply ramps to discontinuities
81   if (ramp_n > 0)
82     ## remember original frequency points prior to applying ramps
83     basef = f(:); basem = m(:);
84
85     ## separate identical frequencies, but keep the midpoint
86     idx = find (diff(f) == 0);
87     f(idx) = f(idx) - ramp_n/grid_n/2;
88     f(idx+1) = f(idx+1) + ramp_n/grid_n/2;
89     f = [f(:);basef(idx)]';
90
91     ## make sure the grid points stay monotonic in [0,1]
92     f(f<0) = 0;
93     f(f>1) = 1;
94     f = unique([f(:);basef(idx)(:)]');
95
96     ## preserve window shape even though f may have changed
97     m = interp1(basef, basem, f);
98
99     # axis([-.1 1.1 -.1 1.1])
100     # plot(f,m,'-xb;ramped;',basef,basem,'-or;original;'); pause;
101   endif
102
103   ## interpolate between grid points
104   grid = interp1(f,m,linspace(0,1,grid_n+1)');
105   # hold on; plot(linspace(0,1,grid_n+1),grid,'-+g;grid;'); hold off; pause;
106
107   ## Transform frequency response into time response and
108   ## center the response about n/2, truncating the excess
109   if (rem(n,2) == 0)
110     b = ifft([grid ; grid(grid_n:-1:2)]);
111     mid = (n+1)/2;
112     b = real ([ b([end-floor(mid)+1:end]) ; b(1:ceil(mid)) ]);
113   else
114     ## Add zeros to interpolate by 2, then pick the odd values below.
115     b = ifft([grid ; zeros(grid_n*2,1) ;grid(grid_n:-1:2)]);
116     b = 2 * real([ b([end-n+1:2:end]) ; b(2:2:(n+1))]);
117   endif
118   ## Multiplication in the time domain is convolution in frequency,
119   ## so multiply by our window now to smooth the frequency response.
120   if rows(window) > 1
121     b = b .* window;
122   else
123     b = b' .* window;
124   endif
125 endfunction
126
127 %!demo
128 %! f=[0, 0.3, 0.3, 0.6, 0.6, 1]; m=[0, 0, 1, 1/2, 0, 0];
129 %! [h, w] = freqz(fir2(100,f,m));
130 %! subplot(121);
131 %! plot(f,m,';target response;',w/pi,abs(h),';filter response;');
132 %! subplot(122);
133 %! plot(f,20*log10(m+1e-5),';target response (dB);',...
134 %!      w/pi,20*log10(abs(h)),';filter response (dB);');
135
136 %!demo
137 %! f=[0, 0.3, 0.3, 0.6, 0.6, 1]; m=[0, 0, 1, 1/2, 0, 0];
138 %! plot(f,20*log10(m+1e-5),';target response;');
139 %! hold on;
140 %! [h, w] = freqz(fir2(50,f,m,512,0));
141 %! plot(w/pi,20*log10(abs(h)),';filter response (ramp=0);');
142 %! [h, w] = freqz(fir2(50,f,m,512,25.6));
143 %! plot(w/pi,20*log10(abs(h)),';filter response (ramp=pi/20 rad);');
144 %! [h, w] = freqz(fir2(50,f,m,512,51.2));
145 %! plot(w/pi,20*log10(abs(h)),';filter response (ramp=pi/10 rad);');
146 %! hold off;
147
148 %!demo
149 %! % Classical Jakes spectrum
150 %! % X represents the normalized frequency from 0
151 %! % to the maximum Doppler frequency
152 %! asymptote = 2/3;
153 %! X = linspace(0,asymptote-0.0001,200);
154 %! Y = (1 - (X./asymptote).^2).^(-1/4);
155 %!
156 %! % The target frequency response is 0 after the asymptote
157 %! X = [X, asymptote, 1];
158 %! Y = [Y, 0, 0];
159 %!
160 %! title('Theoretical/Synthesized CLASS spectrum');
161 %! xlabel('Normalized frequency (Fs=2)');
162 %! ylabel('Magnitude');
163 %!
164 %! plot(X,Y,'b;Target spectrum;');
165 %! hold on;
166 %! [H,F]=freqz(fir2(20, X, Y));
167 %! plot(F/pi,abs(H),'c;Synthesized spectrum (n=20);');
168 %! [H,F]=freqz(fir2(50, X, Y));
169 %! plot(F/pi,abs(H),'r;Synthesized spectrum (n=50);');
170 %! [H,F]=freqz(fir2(200, X, Y));
171 %! plot(F/pi,abs(H),'g;Synthesized spectrum (n=200);');
172 %! hold off;
173 %! xlabel(''); ylabel(''); title('');