1 /* GStreamer
2 * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23
24 #include <glib.h>
25 #include <math.h>
26
27 #include "_kiss_fft_guts_s32.h"
28 #include "kiss_fftr_s32.h"
29 #include "gstfft.h"
30 #include "gstffts32.h"
31
32 /**
33 * SECTION:gstffts32
34 * @title: GstFFTS32
35 * @short_description: FFT functions for signed 32 bit integer samples
36 *
37 * #GstFFTS32 provides a FFT implementation and related functions for
38 * signed 32 bit integer samples. To use this call gst_fft_s32_new() for
39 * allocating a #GstFFTS32 instance with the appropriate parameters and
40 * then call gst_fft_s32_fft() or gst_fft_s32_inverse_fft() to perform the
41 * FFT or inverse FFT on a buffer of samples.
42 *
43 * After use free the #GstFFTS32 instance with gst_fft_s32_free().
44 *
45 * For the best performance use gst_fft_next_fast_length() to get a
46 * number that is entirely a product of 2, 3 and 5 and use this as the
47 * @len parameter for gst_fft_s32_new().
48 *
49 * The @len parameter specifies the number of samples in the time domain that
50 * will be processed or generated. The number of samples in the frequency domain
51 * is @len/2 + 1. To get n samples in the frequency domain use 2*n - 2 as @len.
52 *
53 * Before performing the FFT on time domain data it usually makes sense
54 * to apply a window function to it. For this gst_fft_s32_window() can comfortably
55 * be used.
56 *
57 * Be aware, that you can't simply run gst_fft_s32_inverse_fft() on the
58 * resulting frequency data of gst_fft_s32_fft() to get the original data back.
59 * The relation between them is iFFT (FFT (x)) = x / nfft where nfft is the
60 * length of the FFT. This also has to be taken into account when calculation
61 * the magnitude of the frequency data.
62 */
63
64 struct _GstFFTS32
65 {
66 void *cfg;
67 gboolean inverse;
68 gint len;
69 };
70
71 /**
72 * gst_fft_s32_new: (skip)
73 * @len: Length of the FFT in the time domain
74 * @inverse: %TRUE if the #GstFFTS32 instance should be used for the inverse FFT
75 *
76 * This returns a new #GstFFTS32 instance with the given parameters. It makes
77 * sense to keep one instance for several calls for speed reasons.
78 *
79 * @len must be even and to get the best performance a product of
80 * 2, 3 and 5. To get the next number with this characteristics use
81 * gst_fft_next_fast_length().
82 *
83 * Returns: a new #GstFFTS32 instance.
84 */
85 GstFFTS32 *
gst_fft_s32_new(gint len,gboolean inverse)86 gst_fft_s32_new (gint len, gboolean inverse)
87 {
88 GstFFTS32 *self;
89 gsize subsize = 0, memneeded;
90
91 g_return_val_if_fail (len > 0, NULL);
92 g_return_val_if_fail (len % 2 == 0, NULL);
93
94 kiss_fftr_s32_alloc (len, (inverse) ? 1 : 0, NULL, &subsize);
95 memneeded = ALIGN_STRUCT (sizeof (GstFFTS32)) + subsize;
96
97 self = (GstFFTS32 *) g_malloc0 (memneeded);
98
99 self->cfg = (((guint8 *) self) + ALIGN_STRUCT (sizeof (GstFFTS32)));
100 self->cfg = kiss_fftr_s32_alloc (len, (inverse) ? 1 : 0, self->cfg, &subsize);
101 g_assert (self->cfg);
102
103 self->inverse = inverse;
104 self->len = len;
105
106 return self;
107 }
108
109 /**
110 * gst_fft_s32_fft:
111 * @self: #GstFFTS32 instance for this call
112 * @timedata: Buffer of the samples in the time domain
113 * @freqdata: Target buffer for the samples in the frequency domain
114 *
115 * This performs the FFT on @timedata and puts the result in @freqdata.
116 *
117 * @timedata must have as many samples as specified with the @len parameter while
118 * allocating the #GstFFTS32 instance with gst_fft_s32_new().
119 *
120 * @freqdata must be large enough to hold @len/2 + 1 #GstFFTS32Complex frequency
121 * domain samples.
122 *
123 */
124 void
gst_fft_s32_fft(GstFFTS32 * self,const gint32 * timedata,GstFFTS32Complex * freqdata)125 gst_fft_s32_fft (GstFFTS32 * self, const gint32 * timedata,
126 GstFFTS32Complex * freqdata)
127 {
128 g_return_if_fail (self);
129 g_return_if_fail (!self->inverse);
130 g_return_if_fail (timedata);
131 g_return_if_fail (freqdata);
132
133 kiss_fftr_s32 (self->cfg, timedata, (kiss_fft_s32_cpx *) freqdata);
134 }
135
136 /**
137 * gst_fft_s32_inverse_fft:
138 * @self: #GstFFTS32 instance for this call
139 * @freqdata: Buffer of the samples in the frequency domain
140 * @timedata: Target buffer for the samples in the time domain
141 *
142 * This performs the inverse FFT on @freqdata and puts the result in @timedata.
143 *
144 * @freqdata must have @len/2 + 1 samples, where @len is the parameter specified
145 * while allocating the #GstFFTS32 instance with gst_fft_s32_new().
146 *
147 * @timedata must be large enough to hold @len time domain samples.
148 *
149 */
150 void
gst_fft_s32_inverse_fft(GstFFTS32 * self,const GstFFTS32Complex * freqdata,gint32 * timedata)151 gst_fft_s32_inverse_fft (GstFFTS32 * self, const GstFFTS32Complex * freqdata,
152 gint32 * timedata)
153 {
154 g_return_if_fail (self);
155 g_return_if_fail (self->inverse);
156 g_return_if_fail (timedata);
157 g_return_if_fail (freqdata);
158
159 kiss_fftri_s32 (self->cfg, (kiss_fft_s32_cpx *) freqdata, timedata);
160 }
161
162 /**
163 * gst_fft_s32_free:
164 * @self: #GstFFTS32 instance for this call
165 *
166 * This frees the memory allocated for @self.
167 *
168 */
169 void
gst_fft_s32_free(GstFFTS32 * self)170 gst_fft_s32_free (GstFFTS32 * self)
171 {
172 g_free (self);
173 }
174
175 /**
176 * gst_fft_s32_window:
177 * @self: #GstFFTS32 instance for this call
178 * @timedata: Time domain samples
179 * @window: Window function to apply
180 *
181 * This calls the window function @window on the @timedata sample buffer.
182 *
183 */
184 void
gst_fft_s32_window(GstFFTS32 * self,gint32 * timedata,GstFFTWindow window)185 gst_fft_s32_window (GstFFTS32 * self, gint32 * timedata, GstFFTWindow window)
186 {
187 gint i, len;
188
189 g_return_if_fail (self);
190 g_return_if_fail (timedata);
191
192 len = self->len;
193
194 switch (window) {
195 case GST_FFT_WINDOW_RECTANGULAR:
196 /* do nothing */
197 break;
198 case GST_FFT_WINDOW_HAMMING:
199 for (i = 0; i < len; i++)
200 timedata[i] *= (0.53836 - 0.46164 * cos (2.0 * G_PI * i / len));
201 break;
202 case GST_FFT_WINDOW_HANN:
203 for (i = 0; i < len; i++)
204 timedata[i] *= (0.5 - 0.5 * cos (2.0 * G_PI * i / len));
205 break;
206 case GST_FFT_WINDOW_BARTLETT:
207 for (i = 0; i < len; i++)
208 timedata[i] *= (1.0 - fabs ((2.0 * i - len) / len));
209 break;
210 case GST_FFT_WINDOW_BLACKMAN:
211 for (i = 0; i < len; i++)
212 timedata[i] *= (0.42 - 0.5 * cos ((2.0 * i) / len) +
213 0.08 * cos ((4.0 * i) / len));
214 break;
215 default:
216 g_assert_not_reached ();
217 break;
218 }
219 }
220