annotate src/dmfft.c @ 1315:7687412f9aef

Fix jssmod sample conversion flags storing .. urgh.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 20 Aug 2017 01:54:54 +0300
parents f077a709d491
children e68f7b0cb536
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
749
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Realfftf
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Originally (C) Copyright 1993 Philib Van Baren
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * Cleaned up and refactored to be re-entrant by
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 * Matti 'ccr' Hamalainen <ccr@tnsp.org>, 2013
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 *
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 * Note: Output is BIT-REVERSED! so you must use the BitReversed to
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 * get legible output, (i.e. Real_i = buffer[ BitReversed[i] ]
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 * Imag_i = buffer[ BitReversed[i]+1 ] )
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 * Input is in normal order.
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 */
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include "dmfft.h"
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include <math.h>
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 int dmInitializeFFT(DMFFTContext * ctx, const int fftlen)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 int i;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 if (ctx == NULL)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 return DMERR_NULLPTR;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 if (fftlen <= 16)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 return DMERR_INVALID_ARGS;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 // Allocate necessary tables
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 ctx->npoints = fftlen / 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 ctx->sinTable = (DMFFTType *) dmMalloc(fftlen * sizeof(DMFFTType));
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 if (ctx->sinTable == NULL)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 return DMERR_MALLOC;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 // Bitreversion buffer
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 ctx->breversed = (int *) dmMalloc(ctx->npoints * sizeof(int));
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 if (ctx->breversed == NULL)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 dmFree(ctx->sinTable);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 return DMERR_MALLOC;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 // Calculate data
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 for (i = 0; i < ctx->npoints; i++)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 int mask, tmp;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 for (tmp = 0, mask = ctx->npoints / 2; mask > 0; mask >>= 1)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 tmp = (tmp >> 1) + ((i & mask) ? ctx->npoints : 0);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 ctx->breversed[i] = tmp;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 for (i = 0; i < ctx->npoints; i++)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 {
751
817e792d9360 Use DM_PI instead of M_PI.
Matti Hamalainen <ccr@tnsp.org>
parents: 750
diff changeset
54 ctx->sinTable[ctx->breversed[i]] = -sin((2.0f * DM_PI * i) / fftlen);
749
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 ctx->sinTable[ctx->breversed[i] + 1] =
751
817e792d9360 Use DM_PI instead of M_PI.
Matti Hamalainen <ccr@tnsp.org>
parents: 750
diff changeset
56 -cos((2.0f * DM_PI * i) / fftlen);
749
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 return DMERR_OK;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 void dmEndFFT(DMFFTContext * ctx)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 if (ctx != NULL)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 dmFree(ctx->breversed);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 dmFree(ctx->sinTable);
1167
848a88ce7a57 Use dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 1102
diff changeset
69 dmMemset(ctx, 0, sizeof(DMFFTContext));
749
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 int dmRealFFT(DMFFTContext * ctx, DMFFTType * buffer)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 if (ctx == NULL || buffer == NULL)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 return DMERR_NULLPTR;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
1252
f077a709d491 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
79 const int *br1 = ctx->breversed + 1;
f077a709d491 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
80 const int *br2 = ctx->breversed + ctx->npoints - 1;
749
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 int rounds = ctx->npoints / 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 DMFFTType *endptr1 = buffer + ctx->npoints * 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 while (rounds > 0)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 DMFFTType *sptr = ctx->sinTable;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 DMFFTType *A = buffer,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 *B = buffer + rounds * 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 while (A < endptr1)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 DMFFTType qsin = *sptr;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 DMFFTType qcos = *(sptr + 1);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 DMFFTType *endptr2 = B;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 while (A < endptr2)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 DMFFTType v1 = (*B) * qcos + (*(B + 1)) * qsin;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 DMFFTType v2 = (*B) * qsin - (*(B + 1)) * qcos;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 *B = (*A + v1) * 0.5;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 *(A++) = *(B++) - v1;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 *B = (*A - v2) * 0.5;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 *(A++) = *(B++) + v2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 A = B;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 B += rounds * 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 sptr += 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 rounds >>= 1;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
1252
f077a709d491 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
113 // Massage output to get the output for a real input sequence.
749
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 while (br1 <= br2)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 const DMFFTType vsin = ctx->sinTable[*br1];
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 const DMFFTType vcos = ctx->sinTable[*br1 + 1];
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 DMFFTType *A = buffer + *br1,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 *B = buffer + *br2,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 HRplus, HRminus,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 HIplus, HIminus,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 tmp1, tmp2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 HRminus = *A - *B;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 HRplus = HRminus + (*B * 2);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 HIminus = *(A + 1) - *(B + 1);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 HIplus = HIminus + (*(B + 1) * 2);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 tmp1 = (vsin * HRminus) - (vcos * HIplus);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 tmp2 = (vcos * HRminus) + (vsin * HIplus);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 *A = (HRplus + tmp1) * 0.5f;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 *B = *A - tmp1;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 *(A + 1) = (HIminus + tmp2) * 0.5f;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 *(B + 1) = (*(A + 1)) - HIminus;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 br1++;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 br2--;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 // Handle DC bin separately
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 buffer[0] += buffer[1];
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 buffer[1] = 0;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 return DMERR_OK;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 }
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
150
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
151
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
152 int dmConvertFFTtoFreqDomain(DMFFTContext *ctx, DMFFTType *buffer,
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
153 DMFFTType *real, DMFFTType *imag)
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
154 {
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
155 int i;
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
156 if (ctx == NULL || buffer == NULL || real == NULL || imag == NULL)
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
157 return DMERR_NULLPTR;
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
158
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
159 // Convert from bitreversed form to normal frequency domain
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
160 const int *breversed = ctx->breversed;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
161
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
162 for (i = 1; i < ctx->npoints; i++)
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
163 {
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
164 real[i] = buffer[breversed[i] ];
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
165 imag[i] = buffer[breversed[i]+1];
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
166 }
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
167
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
168 // Make the ends meet
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
169 real[0] = buffer[0];
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
170 imag[0] = 0;
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
171 real[ctx->npoints] = buffer[1];
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
172 imag[ctx->npoints] = 0;
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
173
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
174 return DMERR_OK;
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
175 }
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
176
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
177
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
178 #define DM_PW(a, b, c) sqrt(a * a + b * b) * c
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
179
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
180
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
181 int dmConvertFFTtoFreqAndPower(DMFFTContext *ctx, DMFFTType *buffer,
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
182 DMFFTType *real, DMFFTType *imag, DMFFTType *power, const DMFFTType scale)
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
183 {
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
184 int i;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
185 if (ctx == NULL || buffer == NULL || real == NULL || imag == NULL || power == NULL)
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
186 return DMERR_NULLPTR;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
187
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
188 // Convert from bitreversed form to normal frequency domain
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
189 const int *breversed = ctx->breversed;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
190
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
191 for (i = 1; i < ctx->npoints; i++)
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
192 {
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
193 DMFFTType fre = real[i] = buffer[breversed[i] ],
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
194 fim = imag[i] = buffer[breversed[i]+1];
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
195
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
196 power[i] = DM_PW(fre, fim, scale);
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
197 }
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
198
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
199 // Make the ends meet
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
200 real[0] = buffer[0];
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
201 imag[0] = 0;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
202 power[0] = DM_PW(buffer[0], 0, scale);
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
203
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
204 real[ctx->npoints] = buffer[1];
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
205 imag[ctx->npoints] = 0;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
206 power[ctx->npoints] = DM_PW(buffer[1], 0, scale);
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
207
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
208 return DMERR_OK;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
209 }
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
210
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
211
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
212 int dmConvertFFTtoPowerAndSum(DMFFTContext *ctx, DMFFTType *buffer,
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
213 DMFFTType *power, const DMFFTType pscale, DMFFTType *sum, const DMFFTType sscale)
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
214 {
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
215 int i;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
216 if (ctx == NULL || buffer == NULL || power == NULL || sum == NULL)
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
217 return DMERR_NULLPTR;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
218
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
219 // Convert from bitreversed form to normal frequency domain
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
220 const int *breversed = ctx->breversed;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
221 DMFFTType psum = 0;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
222
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
223 for (i = 1; i < ctx->npoints; i++)
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
224 {
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
225 DMFFTType fre = buffer[breversed[i] ],
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
226 fim = buffer[breversed[i]+1],
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
227 fpw = sqrt(fre * fre + fim * fim);
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
228
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
229 power[i] = fpw * pscale;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
230 psum += fpw * sscale;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
231 }
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
232
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
233 // Make the ends meet
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
234 power[0] = DM_PW(buffer[0], 0, pscale);
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
235 power[ctx->npoints] = DM_PW(buffer[1], 0, pscale);
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
236
773
95d9bd557a54 Return the power sum variable in the FFT conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 772
diff changeset
237 *sum = psum;
95d9bd557a54 Return the power sum variable in the FFT conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 772
diff changeset
238
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
239 return DMERR_OK;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
240 }
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
241
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
242
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
243 int dmConvertFFTtoTimeDomain(DMFFTContext *ctx, DMFFTType *buffer, DMFFTType *tdom)
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
244 {
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
245 int i, n;
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
246 if (ctx == NULL || buffer == NULL || tdom == NULL)
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
247 return DMERR_NULLPTR;
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
248
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
249 for (n = i = 0; i < ctx->npoints; i++, n += 2)
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
250 {
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
251 tdom[n ] = buffer[ctx->breversed[i] ];
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
252 tdom[n+1] = buffer[ctx->breversed[i]+1];
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
253 }
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
254
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
255 return DMERR_OK;
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
256 }