gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
IIR.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 #include "DSP/IIR.H"
18 
20 {
21 // std::cout<<__func__<<std::endl;
22 }
23 
25 {
26  //dtor
27 }
28 
29 // int IIR::reset(const Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> &Bin, const Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> &Ain, Eigen::Dynamic, Eigen::Dynamic> &memIn){
30 // int ret=reset(Bin, Ain);
31 // if (ret!=0)
32 // return ret;
33 // return setMem(memIn);
34 // }
35 //
36 int IIR::reset(const Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> &Bin, const Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> &Ain){
37 // if (!(A.row(0).array()==Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>::Ones(1,A.cols())).all())
38  if (!(Ain.row(0).array()==1.0).all())
40  if (Ain.cols()!=Bin.cols())
42  B=Bin;
43  A=Ain;
44  int maxRows=std::max(B.rows(),A.rows());
45  mem=Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(maxRows, A.cols());
46  return 0;
47 }
48 
49 int IIR::setMem(const Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> &memIn){
50  if (mem.cols()!=memIn.cols())
52  if (mem.rows()!=memIn.rows())
54  mem=memIn;
55  return 0;
56 }
57 
58 int IIR::setMem(const IIR &iir){
59  if (mem.rows()!=iir.mem.rows())
61  unsigned int ch=std::min(mem.cols(), iir.mem.cols());
62  mem.block(0,0,mem.rows(),ch)=iir.mem.block(0,0,mem.rows(),ch);
63  return 0;
64 }
65 
66 int IIR::process(const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> &x, Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> const &y){
67  if (x.cols()!=A.cols()){
68  printf("Input channel count %lld mismatch to filter channel count %lld", (long long)x.cols(), (long long)A.cols());
70  }
71  if (y.cols()!=A.cols()){
72  printf("Output channel count %lld mismatch to filter channel count %lld", (long long)y.cols(), (long long)A.cols());
74  }
75  if (x.rows()!=y.rows()){
76  printf("Input sample count %lld not equal to output sample count %lld", (long long)x.rows(), (long long)y.rows());
78  }
79 
80  if (y.rows() != yTemp.rows() && y.cols() != yTemp.cols())
81  yTemp.resize(y.rows(), y.cols());
82 
83  for (int i=0; i<x.rows(); i++){
84  mem.row(0)=-x.row(i);
85  mem.row(0)=-(A*mem.block(0, 0, A.rows(), A.cols())).colwise().sum();
86  yTemp.row(i)=(B*mem.block(0, 0, B.rows(), B.cols())).colwise().sum();
87  for (int j=mem.rows()-1; j>0; j--)
88  mem.row(j)=mem.row(j-1);
89  }
90 
91  const_cast< Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& >(y)=yTemp;
92  return 0;
93 }
94 
95 int IIR::process(const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> &x, Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> const &y,
96  const Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> &BStep, const Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic> &AStep){
97  if (x.cols()!=A.cols()){
98  printf("Input channel count %lld mismatch to filter channel count %lld", (long long)x.cols(), (long long)A.cols());
100  }
101  if (y.cols()!=A.cols()){
102  printf("Output channel count %lld mismatch to filter channel count %lld", (long long)y.cols(), (long long)A.cols());
104  }
105  if (x.rows()!=y.rows()){
106  printf("Input sample count %lld not equal to output sample count %lld", (long long)x.rows(), (long long)y.rows());
108  }
109 
110  if ((BStep.cols()!=B.cols()) || (AStep.cols()!=A.cols()) || (B.cols()!=A.cols())){
111  printf("BStep or AStep channel count (%lld, %lld) mismatch to filter channel count %lld", (long long)BStep.cols(), (long long)AStep.cols(), (long long)A.cols());
113  }
114 
115  if ((BStep.rows()!=B.rows()) || (AStep.rows()!=A.rows())){
116  printf("BStep order %lld not equal to B order %lld", (long long)BStep.rows(), (long long)B.rows());
117  printf("OR AStep order %lld not equal to A order %lld", (long long)AStep.rows(), (long long)A.rows());
119  }
120 
121  if (y.rows() != yTemp.rows() && y.cols() != yTemp.cols())
122  yTemp.resize(y.rows(), y.cols());
123 
124  for (int i=0; i<x.rows(); i++){
125  mem.row(0)=-x.row(i);
126  mem.row(0)=-(A*mem.block(0, 0, A.rows(), A.cols())).colwise().sum();
127  yTemp.row(i)=(B*mem.block(0, 0, B.rows(), B.cols())).colwise().sum();
128  for (int j=mem.rows()-1; j>0; j--)
129  mem.row(j)=mem.row(j-1);
130  B+=BStep; // step the filter coefficients on
131  A+=AStep;
132  }
133 
134  const_cast< Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>& >(y)=yTemp;
135  return 0;
136 }
137 
138 
139 // void IIR::copyTo(IIR iirIn){
140 // if (B.cols()!=iirIn.B.cols()){
141 // printf("B channel count %lld mismatch to iirIn.B channel count %lld", (long long)B.cols(), (long long)iirIn.B.cols());
142 // return IIRDebug().evaluateError(IIR_CH_CNT_ERROR);
143 // }
144 // if (A.cols()!=iirIn.A.cols()){
145 // printf("A channel count %lld mismatch to iirIn.A channel count %lld", (long long)A.cols(), (long long)iirIn.A.cols());
146 // return IIRDebug().evaluateError(IIR_CH_CNT_ERROR);
147 // }
148 // if (mem.cols()!=iirIn.mem.cols()){
149 // printf("mem channel count %lld mismatch to iirIn.mem channel count %lld", (long long)mem.cols(), (long long)iirIn.mem.cols());
150 // return IIRDebug().evaluateError(IIR_CH_CNT_ERROR);
151 // }
152 // unsigned int ch=::min(B.cols(), iirIn.B.cols());
153 // iirIn.B.block(0,0,iirIn.B.rows(),ch)=B.block(0,0,B.rows(),ch);
154 // iirIn.A.block(0,0,iirIn.A.rows(),ch)=A.block(0,0,A.rows(),ch);
155 // iirIn.mem.block(0,0,iirIn.mem.rows(),ch)=mem.block(0,0,mem.rows(),ch);
156 // }
int reset()
Definition: IIR.H:61
IIR()
Definition: IIR.C:19
float * x
int setMem(const Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic > &memIn)
Definition: IIR.C:49
virtual int evaluateError(int errorNum)
Definition: Debug.H:132
Definition: IIR.H:29
Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic > A
Definition: IIR.H:52
Definition: IIR.H:49
Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic > B
Definition: IIR.H:51
virtual ~IIR()
Definition: IIR.C:24
float * y
#define IIR_CH_CNT_ERROR
Channel count mismatch error.
Definition: IIR.H:24
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > yTemp
Definition: IIR.H:53
Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic > mem
Definition: IIR.H:54
#define IIR_A0_ERROR
Error when feedback coefficient A0 is not = 1.
Definition: IIR.H:23
#define IIR_N_CNT_ERROR
Channel count mismatch error.
Definition: IIR.H:25
int process(const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > &x, Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > const &y)
Definition: IIR.C:66
gtkIOStream: /tmp/gtkiostream/src/DSP/IIR.C Source File
GTK+ IOStream  Beta