-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfilter.pas
233 lines (219 loc) · 8.6 KB
/
filter.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
{
publish with BSD Licence.
Copyright (c) Terry Lao
}
unit filter;
{$MODE Delphi}
interface
uses iLBC_define,C2Delphi_header;
{----------------------------------------------------------------*
* all-pole filter
*---------------------------------------------------------------}
procedure AllPoleFilter(
InOut:PAreal; { (i/o) on entrance InOut[-orderCoef] to
InOut[-1] contain the state of the
filter (delayed samples). InOut[0] to
InOut[lengthInOut-1] contain the filter
input, on en exit InOut[-orderCoef] to
InOut[-1] is unchanged and InOut[0] to
InOut[lengthInOut-1] contain filtered
samples }
Coef:PAreal;{ (i) filter coefficients, Coef[0] is assumed
to be 1.0 }
lengthInOut:integer;{ (i) number of input/output samples }
orderCoef:integer { (i) number of filter coefficients }
);
procedure AllZeroFilter(
nIn:PAreal; { (i) In[0] to In[lengthInOut-1] contain
filter input samples }
Coef:PAreal;{ (i) filter coefficients (Coef[0] is assumed
to be 1.0) }
lengthInOut:integer;{ (i) number of input/output samples }
orderCoef:integer; { (i) number of filter coefficients }
nOut:PAreal { (i/o) on entrance Out[-orderCoef] to Out[-1]
contain the filter state, on exit Out[0]
to Out[lengthInOut-1] contain filtered
samples }
);
procedure ZeroPoleFilter(
nIn:PAreal; { (i) In[0] to In[lengthInOut-1] contain
filter input samples In[-orderCoef] to
In[-1] contain state of all-zero
section }
ZeroCoef:pareal;{ (i) filter coefficients for all-zero
section (ZeroCoef[0] is assumed to
be 1.0) }
PoleCoef:pareal;{ (i) filter coefficients for all-pole section
(ZeroCoef[0] is assumed to be 1.0) }
lengthInOut:integer;{ (i) number of input/output samples }
orderCoef:integer; { (i) number of filter coefficients }
nOut:pareal { (i/o) on entrance Out[-orderCoef] to Out[-1]
contain state of all-pole section. On
exit Out[0] to Out[lengthInOut-1]
contain filtered samples }
);
procedure DownSample (
nIn:PAreal; { (i) input samples }
Coef:pareal; { (i) filter coefficients }
lengthIn:integer; { (i) number of input samples }
state:pareal; { (i) filter state }
nOut:PAreal { (o) downsampled output }
);
implementation
procedure AllPoleFilter(
InOut:PAreal; { (i/o) on entrance InOut[-orderCoef] to
InOut[-1] contain the state of the
filter (delayed samples). InOut[0] to
InOut[lengthInOut-1] contain the filter
input, on en exit InOut[-orderCoef] to
InOut[-1] is unchanged and InOut[0] to
InOut[lengthInOut-1] contain filtered
samples }
Coef:PAreal;{ (i) filter coefficients, Coef[0] is assumed
to be 1.0 }
lengthInOut:integer;{ (i) number of input/output samples }
orderCoef:integer { (i) number of filter coefficients }
);
var
n,k:integer;
begin
for n:=0 to lengthInOut-1 do
begin
for k:=1 to orderCoef do
begin
InOut[0] :=InOut[0] - Coef[k]*InOut[-k];
end;
InOut:=@InOut[1];
end;
end;
{----------------------------------------------------------------*
* all-zero filter
*---------------------------------------------------------------}
procedure AllZeroFilter(
nIn:PAreal; { (i) In[0] to In[lengthInOut-1] contain
filter input samples }
Coef:PAreal;{ (i) filter coefficients (Coef[0] is assumed
to be 1.0) }
lengthInOut:integer;{ (i) number of input/output samples }
orderCoef:integer; { (i) number of filter coefficients }
nOut:PAreal { (i/o) on entrance Out[-orderCoef] to Out[-1]
contain the filter state, on exit Out[0]
to Out[lengthInOut-1] contain filtered
samples }
);
var
n,k:integer;
begin
for n:=0 to lengthInOut-1 do
begin
nOut[n] := Coef[0]*nIn[0];
for k:=1 to orderCoef do
begin
nOut[n] :=nOut[n]+ Coef[k]*nIn[-k];
end;
nIn:=@nIn[1];
end;
end;
{----------------------------------------------------------------*
* pole-zero filter
*---------------------------------------------------------------}
procedure ZeroPoleFilter(
nIn:PAreal; { (i) In[0] to In[lengthInOut-1] contain
filter input samples In[-orderCoef] to
In[-1] contain state of all-zero
section }
ZeroCoef:pareal;{ (i) filter coefficients for all-zero
section (ZeroCoef[0] is assumed to
be 1.0) }
PoleCoef:pareal;{ (i) filter coefficients for all-pole section
(ZeroCoef[0] is assumed to be 1.0) }
lengthInOut:integer;{ (i) number of input/output samples }
orderCoef:integer; { (i) number of filter coefficients }
nOut:pareal { (i/o) on entrance Out[-orderCoef] to Out[-1]
contain state of all-pole section. On
exit Out[0] to Out[lengthInOut-1]
contain filtered samples }
);
begin
AllZeroFilter(nIn,ZeroCoef,lengthInOut,orderCoef,nOut);
AllPoleFilter(nOut,PoleCoef,lengthInOut,orderCoef);
end;
{----------------------------------------------------------------*
* downsample (LP filter and decimation)
*---------------------------------------------------------------}
procedure DownSample (
nIn:PAreal; { (i) input samples }
Coef:pareal; { (i) filter coefficients }
lengthIn:integer; { (i) number of input samples }
state:pareal; { (i) filter state }
nOut:PAreal { (o) downsampled output }
);
var
o:real;
Out_ptr:^real;
Coef_ptr, In_ptr:^real;
state_ptr:^real;
i, j, stop:integer;
begin
Out_ptr := @nOut[0];
{ LP filter and decimate at the same time }
i := DELAY_DS;
while ( i < lengthIn) do
begin
Coef_ptr := @Coef[0];
In_ptr := @nIn[i];
state_ptr := @state[FILTERORDER_DS-2];
o := 0.0;
if (i < FILTERORDER_DS) then
stop := i + 1
else
stop := FILTERORDER_DS;
for j := 0 to stop-1 do
begin
o :=o + Coef_ptr^ * In_ptr^;
inc(Coef_ptr);
dec(In_ptr);
end;
for j := i + 1 to FILTERORDER_DS-1 do
begin
o :=o + Coef_ptr^ * (state_ptr^);
inc(Coef_ptr);
dec(state_ptr);
end;
Out_ptr^ := o;
inc(Out_ptr);
i:=i+FACTOR_DS;
end;
{ Get the last part (use zeros as input for the future) }
i:=(lengthIn+FACTOR_DS);
while ( i<(lengthIn+DELAY_DS)) do
begin
o:=0.0;
if (i<lengthIn) then
begin
Coef_ptr := @Coef[0];
In_ptr := @nIn[i];
for j:=0 to FILTERORDER_DS-1 do
begin
o :=o + Coef_ptr^ * Out_ptr^;
inc(Coef_ptr);
dec(Out_ptr);
end;
end
else
begin
Coef_ptr := @Coef[i-lengthIn];
In_ptr := @nIn[lengthIn-1];
for j:=0 to FILTERORDER_DS-(i-lengthIn)-1 do
begin
o := o+ Coef_ptr^ * In_ptr^;
inc(Coef_ptr);
dec(In_ptr);
end;
end;
Out_ptr^ := o;
inc(Out_ptr);
i:=i+FACTOR_DS;
end;
end;
end.