File: xtrxdsp_fft.c

package info (click to toggle)
libxtrxdsp 0.0.1%2Bgit20190830.eec2864-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 268 kB
  • sloc: ansic: 2,538; makefile: 5
file content (49 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download
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
#include <xtrxdsp_fft.h>
#include <math.h>

static unsigned char reverse(unsigned char b) {
	b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
	b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
	b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
	return b;
}

int xtrxdsp_fft_realign_pwr_d(const uint16_t* samples,
							  unsigned fftbins,
							  double scale,
							  double* outv)
{
	if (fftbins != 512)
		return -EINVAL;

	double mulc = scale * 10.0 / 1024 / log2(10);

	for (unsigned i = 0; i < 512; i += 4) {
		uint16_t v[4];
		unsigned idx = reverse(i >> 1);
		v[0] = samples[idx];
		v[1] = samples[idx + 256];
		v[2] = samples[idx + 128];
		v[3] = samples[idx + 384];

		int q[4];
		q[0] = ((int)v[0]) - 65535 + 8192;
		q[1] = ((int)v[1]) - 65535 + 8192;
		q[2] = ((int)v[2]) - 65535 + 8192;
		q[3] = ((int)v[3]) - 65535 + 8192;

		double vv[4];
		vv[0] = mulc * ((double)q[0]);
		vv[1] = mulc * ((double)q[1]);
		vv[2] = mulc * ((double)q[2]);
		vv[3] = mulc * ((double)q[3]);

		unsigned j = i ^ 256;
		outv[j+0] = vv[0];
		outv[j+1] = vv[1];
		outv[j+2] = vv[2];
		outv[j+3] = vv[3];
	}

	return 0;
}