~ubuntu-branches/ubuntu/raring/octave-signal/raring

« back to all changes in this revision

Viewing changes to inst/fir1.m

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2012-09-23 09:14:33 UTC
  • mfrom: (1.1.7)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: package-import@ubuntu.com-20120923091433-wzfir0y7ongjrubp
Tags: upstream-1.2.0
Import upstream version 1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
## Copyright (C) 2000 Paul Kienzle
2
 
##
3
 
## This program is free software; you can redistribute it and/or modify
4
 
## it under the terms of the GNU General Public License as published by
5
 
## the Free Software Foundation; either version 2 of the License, or
6
 
## (at your option) any later version.
7
 
##
8
 
## This program is distributed in the hope that it will be useful,
9
 
## but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
## GNU General Public License for more details.
12
 
##
13
 
## You should have received a copy of the GNU General Public License
14
 
## along with this program; If not, see <http://www.gnu.org/licenses/>.
 
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
15
 
16
16
## usage: b = fir1(n, w [, type] [, window] [, noscale])
17
17
##
51
51
function b = fir1(n, w, varargin)
52
52
 
53
53
  if nargin < 2 || nargin > 5
54
 
    usage("b = fir1(n, w [, type] [, window] [, noscale])");
 
54
    print_usage;
55
55
  endif
56
56
  
57
57
  ## Assign default window, filter type and scale.
59
59
  ## create a lowpass filter.  If multiple band edges, the first band 
60
60
  ## defaults to a stop band so that the two band case defaults to a 
61
61
  ## band pass filter.  Ick.
62
 
  window = []; 
63
 
  scale = 1;
64
 
  ftype = (length(w)==1);       
 
62
  window  = [];
 
63
  scale   = 1;
 
64
  ftype   = (length(w)==1);
65
65
 
66
66
  ## sort arglist, normalize any string
67
67
  for i=1:length(varargin)
68
68
    arg = varargin{i}; 
69
 
    if ischar(arg), arg=tolower(arg);end
 
69
    if ischar(arg), arg=lower(arg);end
70
70
    if isempty(arg) continue; end  # octave bug---can't switch on []
71
71
    switch arg
72
 
      case {'low','stop','dc-1'}, ftype = 1;
73
 
      case {'high','pass','dc-0'}, ftype = 0;
74
 
      case 'scale', scale = 1;
75
 
      case 'noscale', scale = 0;
76
 
      otherwise window = arg;
 
72
      case {'low','stop','dc-1'},             ftype  = 1;
 
73
      case {'high','pass','bandpass','dc-0'}, ftype  = 0;
 
74
      case {'scale'},                         scale  = 1;
 
75
      case {'noscale'},                       scale  = 0;
 
76
      otherwise                               window = arg;
77
77
    end
78
78
  endfor
79
79
 
92
92
  if rem(n,2)==1 && m(2*bands)==1, 
93
93
    warning("n must be even for highpass and bandstop filters. Incrementing.");
94
94
    n = n+1;
95
 
    if isvector(window) && isreal(window)
 
95
    if isvector(window) && isreal(window) && !ischar(window)
96
96
      ## Extend the window using interpolation
97
97
      M = length(window);
98
98
      if M == 1,
99
 
        window = [window; window];
 
99
        window = [window; window];
100
100
      elseif M < 4
101
 
        window = interp1(linspace(0,1,M),window,linspace(0,1,M+1),'linear');
 
101
        window = interp1(linspace(0,1,M),window,linspace(0,1,M+1),'linear');
102
102
      else
103
 
        window = interp1(linspace(0,1,M),window,linspace(0,1,M+1),'spline');
 
103
        window = interp1(linspace(0,1,M),window,linspace(0,1,M+1),'spline');
104
104
      endif
105
105
    endif
106
106
  endif
111
111
  ## normalize filter magnitude
112
112
  if scale == 1
113
113
    ## find the middle of the first band edge
114
 
    if m(1) == 1, w_o = (f(2)-f(1))/2;
115
 
    else w_o = f(3) + (f(4)-f(3))/2;
 
114
    ## find the frequency of the normalizing gain
 
115
    if m(1) == 1
 
116
      ## if the first band is a passband, use DC gain
 
117
      w_o = 0;
 
118
    elseif f(4) == 1
 
119
      ## for a highpass filter,
 
120
      ## use the gain at half the sample frequency
 
121
      w_o = 1;
 
122
    else
 
123
      ## otherwise, use the gain at the center
 
124
      ## frequency of the first passband
 
125
      w_o = f(3) + (f(4)-f(3))/2;
116
126
    endif
117
127
 
118
128
    ## compute |h(w_o)|^-1
130
140
%!demo
131
141
%! freqz(fir1(15,[0.2, 0.5], 'stop', 'noscale'));
132
142
 
133
 
%!assert(fir1(2, .5, 'low', @hanning, 'scale'), [0 1 0]');
134
 
%!assert(fir1(2, .5, 'low', "hanning", 'scale'), [0 1 0]');
135
 
%!assert(fir1(2, .5, 'low', hanning(3), 'scale'), [0 1 0]');
 
143
%!assert(fir1(2, .5, 'low', @hanning, 'scale'), [0 1 0]);
 
144
%!assert(fir1(2, .5, 'low', "hanning", 'scale'), [0 1 0]);
 
145
%!assert(fir1(2, .5, 'low', hanning(3), 'scale'), [0 1 0]);
136
146
 
137
147
%!assert(fir1(10,.5,'noscale'), fir1(10,.5,'low','hamming','noscale'));
138
148
%!assert(fir1(10,.5,'high'), fir1(10,.5,'high','hamming','scale'));
140
150
%!assert(fir1(10,.5,'hanning','scale'), fir1(10,.5,'scale','hanning','low'));
141
151
%!assert(fir1(10,.5,'haNNing','NOscale'), fir1(10,.5,'noscale','Hanning','LOW'));
142
152
%!assert(fir1(10,.5,'boxcar',[]), fir1(10,.5,'boxcar'));
143