gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
FIR.C
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 
18 #include <Sox.H>
19 
20 #include "DSP/FIR.H"
21 
22 int FIR::loadTimeDomainCoefficients(const std::string fileName){
23  int ret=NO_ERROR;
24  Sox<FP_TYPE> sox; // use sox to try to read the filter from file
25  if ((ret=sox.openRead(string(fileName)))<0 && ret!=SOX_READ_MAXSCALE_ERROR) // try to open the file
26  SoxDebug().evaluateError(ret, fileName);
27  else {
28  Eigen::Matrix<FP_TYPE, Eigen::Dynamic, Eigen::Dynamic> hNew; // the time domain representation of the filter
29  if (ret=sox.read(hNew)<0) // Try to read the entire file into the coefficient Matrix B.
30  SoxDebug().evaluateError(ret, fileName);
31  else
33  }
34  return ret;
35 }
36 
37 void FIR::loadTimeDomainCoefficients(const Eigen::Matrix<FP_TYPE, Eigen::Dynamic, Eigen::Dynamic> hIn){
38  h=hIn;
39  resetDFT();
40 }
41 
43  // only reset the DFT if both block size and filter h are defined.
44  if (N==0 || h.rows()<=0 || h.cols() <=0)
45  return;
46  // Find the DFT of h and store in H
47  Eigen::Matrix<FP_TYPE, Eigen::Dynamic, Eigen::Dynamic> hNew(h.rows()+N, h.cols());
48  x.setZero(hNew.rows(), hNew.cols()); // make the input signal the same length as H
49  yTemp.setZero(hNew.rows(), 1); // make the temporary output buffer the same length as H
50  y.setZero(hNew.rows(), hNew.cols()); // make the temporary output buffer the same length as H
51  Y.setZero(hNew.rows(), 1); // make the DFT of the input signal the same length as H
52  hNew.setZero();
53  hNew.topRows(h.rows())=h;
54  H.setZero(hNew.rows(), hNew.cols());
55  for (int i=0; i<hNew.cols(); i++)
56  fft.fwd(H.col(i).data(), hNew.col(i).data(), hNew.rows());
57 }
58 
59 void FIR::init(unsigned int blockSize){
60  N=blockSize;
61  resetDFT();
62 }
#define SOX_READ_MAXSCALE_ERROR
Sox couldn&#39;t open the filename.max to read the rescale value for the audio file.
Definition: Sox.H:36
void resetDFT()
Definition: FIR.C:42
int openRead(string fileName)
Definition: Sox.C:70
Eigen::Matrix< FP_TYPE, Eigen::Dynamic, Eigen::Dynamic > x
the time domain signal for filtering
Definition: FIR.H:65
virtual int evaluateError(int errorNum)
Definition: Debug.H:132
#define NO_ERROR
There is no error.
Definition: Debug.H:33
Eigen::Matrix< FP_TYPE, Eigen::Dynamic, Eigen::Dynamic > h
the time domain representation of the filter
Definition: FIR.H:64
Definition: Sox.H:54
Eigen::Array< Eigen::FFT< FP_TYPE >::Complex, Eigen::Dynamic, Eigen::Dynamic > H
The DFT of the FIR coefficients.
Definition: FIR.H:63
Eigen::Matrix< FP_TYPE, Eigen::Dynamic, Eigen::Dynamic > y
the time domain output signal
Definition: FIR.H:74
Eigen::Matrix< FP_TYPE, Eigen::Dynamic, 1 > yTemp
the time domain signal for filtering
Definition: FIR.H:66
void init(unsigned int blockSize)
Definition: FIR.C:59
Eigen::FFT< FP_TYPE > fft
The fast Fourier transform.
Definition: FIR.H:62
int loadTimeDomainCoefficients(const std::string fileName)
Definition: FIR.C:22
int read(Eigen::DenseBase< Derived > &audioData, int count=0)
Definition: Sox.H:135
Eigen::Array< Eigen::FFT< FP_TYPE >::Complex, Eigen::Dynamic, 1 > Y
the time domain filter output and also the DFT of one col of x
Definition: FIR.H:67
unsigned int N
Block size of the audio subsystem.
Definition: FIR.H:73
gtkIOStream: /tmp/gtkiostream/src/DSP/FIR.C Source File
GTK+ IOStream  Beta