MNE-CPP  beta 1.0
cosinefilter.cpp
Go to the documentation of this file.
1 //=============================================================================================================
37 //*************************************************************************************************************
38 //=============================================================================================================
39 // INCLUDES
40 //=============================================================================================================
41 
42 #include "cosinefilter.h"
43 
44 
45 //*************************************************************************************************************
46 //=============================================================================================================
47 // USED NAMESPACES
48 //=============================================================================================================
49 
50 using namespace UTILSLIB;
51 
52 
53 //*************************************************************************************************************
54 //=============================================================================================================
55 // DEFINE MEMBER METHODS
56 //=============================================================================================================
57 
59 {
60 
61 }
62 
63 
64 //*************************************************************************************************************
65 
66 CosineFilter::CosineFilter(int fftLength, float lowpass, float lowpass_width, float highpass, float highpass_width, double sFreq, TPassType type)
67 {
68  int highpasss,lowpasss;
69  int highpass_widths,lowpass_widths;
70  int k,s,w;
71  int resp_size = fftLength/2+1; //Take half because we are not interested in the conjugate complex part of the spectrum
72 
73  double pi4 = M_PI/4.0;
74  float mult,add,c;
75 
76  RowVectorXcd filterFreqResp = RowVectorXcd::Ones(resp_size);
77 
78  //Transform frequencies into samples
79  highpasss = ((resp_size-1)*highpass)/(0.5*sFreq);
80  lowpasss = ((resp_size-1)*lowpass)/(0.5*sFreq);
81 
82  lowpass_widths = ((resp_size-1)*lowpass_width)/(0.5*sFreq);
83  lowpass_widths = (lowpass_widths+1)/2;
84 
85  if (highpass_width > 0.0) {
86  highpass_widths = ((resp_size-1)*highpass_width)/(0.5*sFreq);
87  highpass_widths = (highpass_widths+1)/2;
88  }
89  else
90  highpass_widths = 3;
91 
92  //Calculate filter freq response - use cosine
93  //Build high pass filter
94  if(type != LPF) {
95  if (highpasss > highpass_widths + 1) {
96  w = highpass_widths;
97  mult = 1.0/w;
98  add = 3.0;
99 
100  for (k = 0; k < highpasss-w+1; k++)
101  filterFreqResp(k) = 0.0;
102 
103  for (k = -w+1, s = highpasss-w+1; k < w; k++, s++) {
104  if (s >= 0 && s < resp_size) {
105  c = cos(pi4*(k*mult+add));
106  filterFreqResp(s) = filterFreqResp(s).real()*c*c;
107  }
108  }
109  }
110  }
111 
112  //Build low pass filter
113  if(type != HPF) {
114  if (lowpass_widths > 0) {
115  w = lowpass_widths;
116  mult = 1.0/w;
117  add = 1.0;
118 
119  for (k = -w+1, s = lowpasss-w+1; k < w; k++, s++) {
120  if (s >= 0 && s < resp_size) {
121  c = cos(pi4*(k*mult+add));
122  filterFreqResp(s) = filterFreqResp(s).real()*c*c;
123  }
124  }
125 
126  for (k = s; k < resp_size; k++)
127  filterFreqResp(k) = 0.0;
128  }
129  else {
130  for (k = lowpasss; k < resp_size; k++)
131  filterFreqResp(k) = 0.0;
132  }
133  }
134 
135  m_dFFTCoeffA = filterFreqResp;
136 
137  //Generate windowed impulse response - invert fft coeeficients to time domain
138  Eigen::FFT<double> fft;
139  fft.SetFlag(fft.HalfSpectrum);
140 
141  //invert to time domain and
142  fft.inv(m_dCoeffA, filterFreqResp);/*
143  m_dCoeffA = m_dCoeffA.segment(0,1024).eval();
144 
145  //window/zero-pad m_dCoeffA to m_iFFTlength
146  RowVectorXd t_coeffAzeroPad = RowVectorXd::Zero(fftLength);
147  t_coeffAzeroPad.head(m_dCoeffA.cols()) = m_dCoeffA;
148 
149  //fft-transform filter coeffs
150  m_dFFTCoeffA = RowVectorXcd::Zero(fftLength);
151  fft.fwd(m_dFFTCoeffA,t_coeffAzeroPad);*/
152 }
153 
154 
155 //*************************************************************************************************************
Declaration of the CosineFilter class.
RowVectorXcd m_dFFTCoeffA
Definition: cosinefilter.h:125