33 template <
typename Tin,
int Zin,
typename Tout,
int Zout,
int Gn,
int Gd>
44 unsigned long count =
min(
in.readable(),
out.writable());
47 for (; pin < pend; ++pin, ++pout)
49 pout->
re = Zout + (pin->re - (Tin)Zin) * Gn / Gd;
50 pout->
im = Zout + (pin->im - (Tin)Zin) * Gn / Gd;
69 for (
int t = n; t > 1; t >>= 1)
73 for (
int i = 0;
i < n; ++
i)
76 for (
int b = 0; b < logn; ++b)
77 bitrev[
i] = (bitrev[
i] << 1) | ((
i >> b) & 1);
82 for (
int i = 0;
i < n; ++
i)
84 float a = 2.0 *
M_PI *
i / n;
85 omega_rev[
i].re = (omega[
i].re = cosf(a));
86 omega_rev[
i].im = -(omega[
i].im = sinf(a));
92 for (
int i = 0;
i < n; ++
i)
104 for (
int i = 0;
i < logn; ++
i)
107 int dom = 1 << (logn - 1 -
i);
108 for (
int j = 0; j < dom; ++j)
110 int p = j * hbs * 2, q = p + hbs;
111 for (
int k = 0; k < hbs; ++k)
117 data[q + k].
re = data[p + k].
re - x.
re;
118 data[q + k].
im = data[p + k].
im - x.
im;
119 data[p + k].
re = data[p + k].
re + x.
re;
120 data[p + k].
im = data[p + k].
im + x.
im;
126 float invn = 1.0 / n;
127 for (
int i = 0;
i < n; ++
i)
142 template <
typename T>
148 in1(_in1), in2(_in2),
out(_out)
153 int n =
out.writable();
154 if (in1.readable() < n)
156 if (in2.readable() < n)
158 T *pin1 = in1.rd(), *pin2 = in2.rd(), *pout =
out.wr(), *pend = pout + n;
160 *pout++ = *pin1++ + *pin2++;
171 template <
typename Tscale,
typename Tin,
typename Tout>
184 unsigned long count =
min(
in.readable(),
out.writable());
185 Tin *pin =
in.rd(), *pend = pin + count;
186 Tout *pout =
out.wr();
187 for (; pin < pend; ++pin, ++pout)
188 *pout = *pin * scale;
200 template <
typename T>
209 int n =
out.writable();
217 x = 2 * drand48() - 1;
218 y = 2 * drand48() - 1;
220 }
while (r2 == 0 || r2 >= 1);
221 float k = sqrtf(-logf(r2) / r2) * stddev;
234 template <
typename T>
244 if (
in.readable() < w)
246 unsigned long count =
min(
in.readable() - w,
out.writable());
247 T *pin =
in.rd(), *pend = pin + count;
250 for (; pin < pend; ++pin, ++pout)
253 for (
int i = 0;
i < w; ++
i)
267 template <
typename T,
typename Tc>
272 unsigned int _decim = 1)
274 ncoeffs(_ncoeffs), coeffs(_coeffs),
277 freq_tap(NULL), tap_multiplier(1), freq_tol(0.1)
279 shifted_coeffs =
new T[ncoeffs];
285 if (
in.readable() < ncoeffs)
290 float new_freq = *freq_tap * tap_multiplier;
291 if (fabs(current_freq - new_freq) > freq_tol)
294 fprintf(stderr,
"Shifting filter %f -> %f\n",
295 current_freq, new_freq);
300 long count =
min((
in.readable() - ncoeffs) / decim,
302 T *pin =
in.rd() + ncoeffs, *pend = pin + count * decim, *pout =
out.wr();
304 for (; pin < pend; pin += decim, ++pout)
306 T *pc = shifted_coeffs;
309 for (
int i = ncoeffs;
i--; ++pc, --pi)
310 x = x + (*pc) * (*pi);
313 in.read(count * decim);
328 for (
int i = 0;
i < ncoeffs; ++
i)
330 float a = 2 *
M_PI * f * (
i - ncoeffs / 2.0);
331 float c = cosf(a), s = sinf(a);
333 shifted_coeffs[
i].re = coeffs[
i] * c;
334 shifted_coeffs[
i].im = coeffs[
i] * s;
347 template <
typename T,
typename Tc>
352 int _interp = 1,
int _decim = 1)
354 ncoeffs(_ncoeffs), coeffs(_coeffs),
355 interp(_interp), decim(_decim),
356 in(_in),
out(_out, interp),
357 freq_tap(NULL), tap_multiplier(1), freq_tol(0.1)
360 fail(
"fir_resampler: decim not implemented");
361 shifted_coeffs =
new T[ncoeffs];
367 if (
in.readable() < ncoeffs)
372 float new_freq = *freq_tap * tap_multiplier;
373 if (fabs(current_freq - new_freq) > freq_tol)
376 fprintf(stderr,
"Shifting filter %f -> %f\n",
377 current_freq, new_freq);
382 if (
in.readable() * interp < ncoeffs)
384 unsigned long count =
min((
in.readable() * interp - ncoeffs) / interp,
385 out.writable() / interp);
386 int latency = (ncoeffs + interp) / interp;
387 T *pin =
in.rd() + latency, *pend = pin + count, *pout =
out.wr();
389 for (; pin < pend; ++pin)
391 for (
int i = 0;
i < interp; ++
i, ++pout)
394 T *pc = shifted_coeffs +
i, *pcend = shifted_coeffs + ncoeffs;
396 for (; pc < pcend; pc += interp, --pi)
397 x = x + (*pc) * (*pi);
402 out.written(count * interp);
422 for (
int i = 0;
i < ncoeffs; ++
i)
424 float a = 2 *
M_PI * f *
i;
425 float c = cosf(a), s = sinf(a);
427 shifted_coeffs[
i].re = coeffs[
i] * c;
428 shifted_coeffs[
i].im = coeffs[
i] * s;
436 #endif // LEANSDR_DSP_H
pipewriter< complex< Tout > > out
pipewriter< complex< T > > out
wgn_c(scheduler *sch, pipebuf< complex< T >> &_out)
scaler(scheduler *sch, Tscale _scale, pipebuf< Tin > &_in, pipebuf< Tout > &_out)
naive_lowpass(scheduler *sch, pipebuf< T > &_in, pipebuf< T > &_out, int _w)
Fixed< IntType, IntBits > sqrt(Fixed< IntType, IntBits > const &x)
adder(scheduler *sch, pipebuf< T > &_in1, pipebuf< T > &_in2, pipebuf< T > &_out)
fir_resampler(scheduler *sch, int _ncoeffs, Tc *_coeffs, pipebuf< T > &_in, pipebuf< T > &_out, int _interp=1, int _decim=1)
fir_filter(scheduler *sch, int _ncoeffs, Tc *_coeffs, pipebuf< T > &_in, pipebuf< T > &_out, unsigned int _decim=1)
pipereader< complex< Tin > > in
cconverter(scheduler *sch, pipebuf< complex< Tin >> &_in, pipebuf< complex< Tout >> &_out)
void inplace(complex< T > *data, bool reverse=false)
T min(const T &x, const T &y)