annotate src/dmfft.c @ 812:1e5cf1144f36

Move library source under src/ subdirectory.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 16 May 2014 03:22:39 +0300
parents dmfft.c@95d9bd557a54
children e06abfde6c39
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);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 memset(ctx, 0, sizeof(DMFFTContext));
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
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 int rounds = ctx->npoints / 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 DMFFTType *endptr1 = buffer + ctx->npoints * 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 while (rounds > 0)
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 DMFFTType *sptr = ctx->sinTable;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 DMFFTType *A = buffer,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 *B = buffer + rounds * 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 while (A < endptr1)
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 DMFFTType qsin = *sptr;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 DMFFTType qcos = *(sptr + 1);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 DMFFTType *endptr2 = B;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 while (A < endptr2)
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 DMFFTType v1 = (*B) * qcos + (*(B + 1)) * qsin;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 DMFFTType v2 = (*B) * qsin - (*(B + 1)) * qcos;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 *B = (*A + v1) * 0.5;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 *(A++) = *(B++) - v1;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 *B = (*A - v2) * 0.5;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 *(A++) = *(B++) + v2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 A = B;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 B += rounds * 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 sptr += 2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 rounds >>= 1;
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
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 * Massage output to get the output for a real input sequence.
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 */
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
114 const int *br1 = ctx->breversed + 1;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
115 const int *br2 = ctx->breversed + ctx->npoints - 1;
749
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 while (br1 <= br2)
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 {
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 const DMFFTType vsin = ctx->sinTable[*br1];
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 const DMFFTType vcos = ctx->sinTable[*br1 + 1];
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 DMFFTType *A = buffer + *br1,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 *B = buffer + *br2,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 HRplus, HRminus,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 HIplus, HIminus,
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 tmp1, tmp2;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 HRminus = *A - *B;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 HRplus = HRminus + (*B * 2);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 HIminus = *(A + 1) - *(B + 1);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 HIplus = HIminus + (*(B + 1) * 2);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 tmp1 = (vsin * HRminus) - (vcos * HIplus);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 tmp2 = (vcos * HRminus) + (vsin * HIplus);
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 *A = (HRplus + tmp1) * 0.5f;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 *B = *A - tmp1;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 *(A + 1) = (HIminus + tmp2) * 0.5f;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 *(B + 1) = (*(A + 1)) - HIminus;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 br1++;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 br2--;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 }
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 // Handle DC bin separately
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 buffer[0] += buffer[1];
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 buffer[1] = 0;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 return DMERR_OK;
44138892c784 Add FFT routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 }
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
152
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
153
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
154 int dmConvertFFTtoFreqDomain(DMFFTContext *ctx, DMFFTType *buffer,
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
155 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
156 {
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
157 int i;
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
158 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
159 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
160
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
161 // 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
162 const int *breversed = ctx->breversed;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
163
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
164 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
165 {
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
166 real[i] = buffer[breversed[i] ];
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
167 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
168 }
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
169
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
170 // 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
171 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
172 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
173 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
174 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
175
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
176 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
177 }
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
178
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
179
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
180 #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
181
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
182
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
183 int dmConvertFFTtoFreqAndPower(DMFFTContext *ctx, DMFFTType *buffer,
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
184 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
185 {
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
186 int i;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
187 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
188 return DMERR_NULLPTR;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
189
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
190 // 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
191 const int *breversed = ctx->breversed;
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 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
194 {
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
195 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
196 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
197
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
198 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
199 }
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
200
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
201 // Make the ends meet
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
202 real[0] = buffer[0];
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
203 imag[0] = 0;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
204 power[0] = DM_PW(buffer[0], 0, scale);
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
205
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
206 real[ctx->npoints] = buffer[1];
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
207 imag[ctx->npoints] = 0;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
208 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
209
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
210 return DMERR_OK;
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
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
213
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
214 int dmConvertFFTtoPowerAndSum(DMFFTContext *ctx, DMFFTType *buffer,
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
215 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
216 {
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
217 int i;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
218 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
219 return DMERR_NULLPTR;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
220
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
221 // 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
222 const int *breversed = ctx->breversed;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
223 DMFFTType psum = 0;
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 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
226 {
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
227 DMFFTType fre = buffer[breversed[i] ],
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
228 fim = buffer[breversed[i]+1],
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
229 fpw = sqrt(fre * fre + fim * fim);
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
230
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
231 power[i] = fpw * pscale;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
232 psum += fpw * sscale;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
233 }
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
234
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
235 // Make the ends meet
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
236 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
237 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
238
773
95d9bd557a54 Return the power sum variable in the FFT conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 772
diff changeset
239 *sum = psum;
95d9bd557a54 Return the power sum variable in the FFT conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 772
diff changeset
240
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
241 return DMERR_OK;
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
242 }
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
243
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
244
750
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
245 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
246 {
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
247 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
248 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
249 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
250
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
251 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
252 {
772
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
253 tdom[n ] = buffer[ctx->breversed[i] ];
59df354b99cc Add some utility functions in the FFT module.
Matti Hamalainen <ccr@tnsp.org>
parents: 751
diff changeset
254 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
255 }
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
256
e6d807ce715b Add two more functions for converting FFT data to frequency and time
Matti Hamalainen <ccr@tnsp.org>
parents: 749
diff changeset
257 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
258 }