]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/nanfilter.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / nanfilter.m
1 function [Y,Z] = nanfilter(B,A,X,z);
2 % NANFILTER is able to filter data with missing values encoded as NaN. 
3 %       
4 %      [Y,Z] = nanfilter(B,A,X [, Z]);  
5 %
6 % If X contains no missing data, NANFILTER should behave like FILTER. 
7 % NaN-values are handled gracefully. 
8 %
9 % WARNING: missing values can introduce aliasing - causing unintended results.
10 %    Moreover, the behavior of bandpass and highpass filters in case of missing values 
11 %    is not fully understood, and might contain some pitfalls.  
12 %
13 % see also: FILTER, SUMSKIPNAN, NANFFT, NANCONV, NANFILTER1UC
14
15 %       $Id$
16 %       Copyright (C) 2005,2011 by Alois Schloegl <alois.schloegl@gmail.com>            
17 %       This function is part of the NaN-toolbox available at 
18 %       http://pub.ist.ac.at/~schloegl/matlab/NaN/ and 
19 %       http://octave.svn.sourceforge.net/viewvc/octave/trunk/octave-forge/extra/NaN/inst/
20
21 %    This program is free software; you can redistribute it and/or modify
22 %    it under the terms of the GNU General Public License as published by
23 %    the Free Software Foundation; either version 3 of the License, or
24 %    (at your option) any later version.
25 %
26 %    This program is distributed in the hope that it will be useful,
27 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
28 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 %    GNU General Public License for more details.
30 %
31 %    You should have received a copy of the GNU General Public License
32 %    along with this program; if not, write to the Free Software
33 %    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
34
35
36 warning('NANFILTER is experimental. For more details see HELP NANFILTER');
37
38 na = length(A);
39 nb = length(B);
40 if any(size(X)==1)
41         nc = 1; 
42 else    
43         nc = size(X,2);
44 end; 
45
46 if nargin<4,
47         [t,Z.S] = filter(B,A,zeros(na+nb,nc));
48         [t,Z.N] = filter(B,A,zeros(na+nb,nc));
49 elseif isnumeric(z),
50         Z.S = z; 
51         [t, Z.N] = filter(B, A, zeros(na+nb,nc));
52 elseif isstruct(z),
53         Z = z; 
54 end;
55
56 NX        = isnan(X);
57 X(NX)     = 0;
58
59 [Y , Z.S] = filter(B, A,   X, Z.S);
60 [NY, Z.N] = filter(B, A, ~NX, Z.N);
61 Y = (sum(B)/sum(A)) * Y./NY;
62