forked from Tsukihime/OpenExpertSDR
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlmadf.c
288 lines (242 loc) · 8.96 KB
/
lmadf.c
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* lmadf.c
This file is part of a program that implements a Software-Defined Radio.
Copyright (C) 2004, 2005, 2006 by Frank Brickle, AB2KT and Bob McGwier, N4HY
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
The authors can be reached by email at
or
or by paper mail at
The DTTS Microwave Society
6 Kathleen Place
Bridgewater, NJ 08807
*/
#include <lmadf.h>
LMSR
new_lmsr(CXB signal,
int delay,
REAL adaptation_rate,
REAL leakage, int adaptive_filter_size, int filter_type)
{
LMSR lms = (LMSR)safealloc(1, sizeof(_lmsstate), "new_lmsr state");
lms->signal = signal;
lms->signal_size = CXBsize(lms->signal);
lms->delay = delay;
lms->size = 512;
lms->mask = lms->size - 1;
lms->delay_line = newvec_REAL(lms->size, "lmsr delay");
lms->adaptation_rate = adaptation_rate;
lms->leakage = leakage;
lms->adaptive_filter_size = adaptive_filter_size;
lms->adaptive_filter = newvec_REAL(128, "lmsr filter");
lms->filter_type = filter_type;
lms->delay_line_ptr = 0;
return (lms);
}
void
del_lmsr(LMSR lms)
{
if(lms)
{
delvec_REAL(lms->delay_line);
delvec_REAL(lms->adaptive_filter);
safefree((char *)lms);
}
}
// just to make the algorithm itself a little clearer,
// get the admin stuff out of the way
#define ssiz (lms->signal_size)
#define asiz (lms->adaptive_filter_size)
#define dptr (lms->delay_line_ptr)
#define rate (lms->adaptation_rate)
#define leak (lms->leakage)
#define ssig(n) (CXBreal(lms->signal,(n)))
#define ssig_i(n) (CXBimag(lms->signal,(n)))
#define dlay(n) (lms->delay_line[(n)])
#define afil(n) (lms->adaptive_filter[(n)])
#define wrap(n) (((n) + (lms->delay) + (lms->delay_line_ptr)) & (lms->mask))
#define bump(n) (((n) + (lms->mask)) & (lms->mask))
static void
lmsr_adapt_i(LMSR lms)
{
int i, j, k;
REAL sum_sq, scl1, scl2;
REAL accum, error;
scl1 = (REAL)(1.0 - rate * leak);
for (i = 0; i < ssiz; i++)
{
dlay(dptr) = ssig(i);
accum = 0.0;
sum_sq = 0.0;
for (j = 0; j < asiz; j++)
{
k = wrap(j);
sum_sq += sqr(dlay(k));
accum += afil(j) * dlay(k);
}
error = ssig(i) - accum;
ssig_i(i) = ssig(i) = error;
scl2 = (REAL)(rate / (sum_sq + 1e-10));
error *= scl2;
for (j = 0; j < asiz; j++)
{
k = wrap(j);
afil(j) = afil(j) * scl1 + error * dlay(k);
}
dptr = bump(dptr);
}
}
static void
lmsr_adapt_n(LMSR lms)
{
int i, j, k;
REAL sum_sq, scl1, scl2;
REAL accum, error;
scl1 = (REAL)(1.0 - rate * leak);
for (i = 0; i < ssiz; i++)
{
dlay(dptr) = ssig(i);
accum = 0.0;
sum_sq = 0.0;
for (j = 0; j < asiz; j++)
{
k = wrap(j);
sum_sq += sqr(dlay(k));
accum += afil(j) * dlay(k);
}
error = ssig(i) - accum;
ssig_i(i) = ssig(i) = accum;
scl2 = (REAL)(rate / (sum_sq + 1e-10));
error *= scl2;
for (j = 0; j < asiz; j++)
{
k = wrap(j);
afil(j) = afil(j) * scl1 + error * dlay(k);
}
dptr = bump(dptr);
}
}
extern void
lmsr_adapt(LMSR lms)
{
switch(lms->filter_type)
{
case LMADF_NOISE:
lmsr_adapt_n(lms);
break;
case LMADF_INTERFERENCE:
lmsr_adapt_i(lms);
break;
}
}
void
del_blms(BLMS blms)
{
if(blms)
{
fftwf_destroy_plan(blms->Xplan);
fftwf_destroy_plan(blms->Yplan);
fftwf_destroy_plan(blms->Errhatplan);
fftwf_destroy_plan(blms->UPDplan);
fftwf_destroy_plan(blms->Wplan);
delvec_COMPLEX(blms->update);
delvec_COMPLEX(blms->Update);
delvec_COMPLEX(blms->What);
delvec_COMPLEX(blms->Xhat);
delvec_COMPLEX(blms->error);
delvec_COMPLEX(blms->Errhat);
delvec_COMPLEX(blms->Yhat);
delvec_COMPLEX(blms->y);
delvec_COMPLEX(blms->delay_line);
safefree((char *)blms);
}
}
BLMS
new_blms(CXB signal, REAL adaptation_rate, REAL leak_rate, int filter_type,
int pbits)
{
BLMS tmp;
tmp = (BLMS)safealloc(1, sizeof(_blocklms), "block lms");
tmp->delay_line = newvec_COMPLEX(256, "block lms delay line");
tmp->y = newvec_COMPLEX(256, "block lms output signal");
tmp->Yhat = newvec_COMPLEX(256, "block lms output transform");
tmp->Errhat = newvec_COMPLEX(256, "block lms Error transform");
tmp->error = newvec_COMPLEX(256, "block lms Error signal");
tmp->Xhat = newvec_COMPLEX(256, "block lms signal transform");
tmp->What = newvec_COMPLEX(256, "block lms filter transform");
tmp->Update = newvec_COMPLEX(256, "block lms update transform");
tmp->update = newvec_COMPLEX(256, "block lms update signal");
tmp->adaptation_rate = adaptation_rate;
tmp->leak_rate = 1.0f - leak_rate;
tmp->signal = signal;
tmp->filter_type = filter_type;
tmp->Xplan = fftwf_plan_dft_1d(256,
(fftwf_complex *)tmp->delay_line,
(fftwf_complex *)tmp->Xhat,
FFTW_FORWARD, pbits);
tmp->Yplan = fftwf_plan_dft_1d(256,
(fftwf_complex *)tmp->Yhat,
(fftwf_complex *)tmp->y,
FFTW_BACKWARD, pbits);
tmp->Errhatplan = fftwf_plan_dft_1d(256,
(fftwf_complex *)tmp->error,
(fftwf_complex *)tmp->Errhat,
FFTW_FORWARD, pbits);
tmp->UPDplan = fftwf_plan_dft_1d(256,
(fftwf_complex *)tmp->Errhat,
(fftwf_complex *)tmp->update,
FFTW_BACKWARD, pbits);
tmp->Wplan = fftwf_plan_dft_1d(256,
(fftwf_complex *)tmp->update,
(fftwf_complex *)tmp->Update,
FFTW_FORWARD, pbits);
return (tmp);
}
#define BLKSCL 1.0f/256.0f
void
blms_adapt(BLMS blms)
{
int sigsize = CXBhave(blms->signal);
int sigidx = 0;
do
{
int j;
memcpy(blms->delay_line, &blms->delay_line[128], sizeof(COMPLEX) * 128); // do overlap move
memcpy(&blms->delay_line[128], &CXBdata(blms->signal, sigidx), sizeof(COMPLEX) * 128); // copy in new data
fftwf_execute(blms->Xplan); // compute transform of input data
for (j = 0; j < 256; j++)
{
blms->Yhat[j] = Cmul(blms->What[j], blms->Xhat[j]); // Filter new signal in freq. domain
blms->Xhat[j] = Conjg(blms->Xhat[j]); // take input data's complex conjugate
}
fftwf_execute(blms->Yplan); //compute output signal transform
for (j = 128; j < 256; j++) blms->y[j] = Cscl(blms->y[j], BLKSCL);
memset(blms->y, 0, 128 * sizeof(COMPLEX));
for (j = 128; j < 256; j++) blms->error[j] = Csub(blms->delay_line[j], blms->y[j]); // compute error signal
if(blms->filter_type) memcpy(&CXBdata(blms->signal, sigidx), &blms->y[128], 128 * sizeof(COMPLEX)); // if noise filter, output y
else memcpy(&CXBdata(blms->signal, sigidx), &blms->error[128], 128 * sizeof(COMPLEX)); // if notch filter, output error
fftwf_execute(blms->Errhatplan); // compute transform of the error signal
for (j = 0; j < 256; j++) blms->Errhat[j] = Cmul(blms->Errhat[j], blms->Xhat[j]); // compute cross correlation transform
fftwf_execute(blms->UPDplan); // compute inverse transform of cross correlation transform
for (j = 0; j < 128; j++) blms->update[j] = Cscl(blms->update[j], BLKSCL);
memset(&blms->update[128], 0, sizeof(COMPLEX) * 128); // zero the last block of the update, so we get
// filter coefficients only at front of buffer
fftwf_execute(blms->Wplan);
for (j = 0; j < 256; j++)
{
blms->What[j] = Cadd(Cscl(blms->What[j], blms->leak_rate), // leak the W away
Cscl(blms->Update[j], blms->adaptation_rate)); // update at adaptation rate
}
sigidx += 128; // move to next block in the signal buffer
}while(sigidx < sigsize); // done?
}