2 * Karinto Library Project
\r
4 * This software is distributed under a zlib-style license.
\r
5 * See license.txt for more information.
\r
9 using System.Collections.Generic;
\r
14 public class WindowFunction
\r
16 #region type definitions
\r
18 public enum FunctionType
\r
35 public delegate double Function(double x);
\r
39 #region private or protected fields
\r
41 private Function function;
\r
42 private static Function[] library;
\r
43 private double parameter;
\r
47 #region constructors
\r
49 static WindowFunction()
\r
51 library = new Function[]{
\r
69 public WindowFunction(FunctionType type)
\r
74 public WindowFunction(FunctionType type, Range range)
\r
80 case FunctionType.Gaussian:
\r
81 function = Gaussian;
\r
84 case FunctionType.Kaiser:
\r
88 case FunctionType.Lanczos:
\r
93 function = library[(int)type];
\r
98 public WindowFunction(FunctionType type, double parameter)
\r
99 : this(type, parameter, null)
\r
103 public WindowFunction(FunctionType type, double parameter, Range range)
\r
107 this.parameter = parameter;
\r
110 case FunctionType.Gaussian:
\r
111 function = Gaussian;
\r
113 case FunctionType.Kaiser:
\r
116 case FunctionType.Lanczos:
\r
117 function = Lanczos;
\r
120 function = library[(int)type];
\r
125 public WindowFunction(Function function)
\r
126 : this(function, null)
\r
130 public WindowFunction(Function function, Range range)
\r
133 Type = FunctionType.UserDefined;
\r
134 this.function = function;
\r
141 public FunctionType Type
\r
155 #region public methods
\r
157 public double[] ApplyTo(double[] target)
\r
159 if (!(Range.First is int)) throw new Exception();
\r
161 int n = (int)Range.Last - (int)Range.First;
\r
162 double[] series = new double[n + (Range.ExcludesEnd ? 0 : 1)];
\r
164 double scale = 1.0 / n;
\r
166 foreach (int x in Range)
\r
168 series[i] = target[x] * function(scale * i);
\r
175 #region private methods
\r
177 private static double BartlettHann(double x)
\r
179 throw new NotImplementedException();
\r
182 private static double Blackman(double x)
\r
184 throw new NotImplementedException();
\r
187 private static double BlackmanNuttall(double x)
\r
189 throw new NotImplementedException();
\r
192 private static double FlatTop(double x)
\r
194 const double a0 = 1.0;
\r
195 const double a1 = 1.93;
\r
196 const double a2 = 1.29;
\r
197 const double a3 = 0.388;
\r
198 const double a4 = 0.032;
\r
199 double theta = 2.0 * Math.PI * x;
\r
200 return a0 - a1 * Cos2Pi(x) + a2 * Cos2Pi(x + x) -
\r
201 a3 * Cos2Pi(3.0 * x) + a4 * Cos2Pi(4.0 * x);
\r
204 private double Gaussian(double x)
\r
206 double r = (x - 0.5) / parameter;
\r
207 return Math.Exp(-r * r);
\r
210 private static double Hamming(double x)
\r
212 return 0.54 - 0.46 * Cos2Pi(x);
\r
215 private static double Hann(double x)
\r
217 return 0.5 - 0.5 * Cos2Pi(x);
\r
220 private double Kaiser(double x)
\r
222 throw new NotImplementedException();
\r
225 private double Lanczos(double x)
\r
227 throw new NotImplementedException();
\r
230 private static double Nuttall(double x)
\r
232 throw new NotImplementedException();
\r
235 private static double Rectangular(double x)
\r
240 private static double Triangular(double x)
\r
242 return (x < 0.5) ? x + x : 2.0 - x - x;
\r
245 private static double Cos2Pi(double x)
\r
247 return Math.Cos(2.0 * Math.PI * x);
\r