2 * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org>
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.
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.
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include "_kiss_fft_guts_f32.h"
24 #include "kiss_fftr_f32.h"
26 #include "gstfftf32.h"
30 * @short_description: FFT functions for 32 bit float samples
32 * #GstFFTF32 provides a FFT implementation and related functions for
33 * 32 bit float samples. To use this call gst_fft_f32_new() for
34 * allocating a #GstFFTF32 instance with the appropriate parameters and
35 * then call gst_fft_f32_fft() or gst_fft_f32_inverse_fft() to perform the
36 * FFT or inverse FFT on a buffer of samples.
38 * After use free the #GstFFTF32 instance with gst_fft_f32_free().
40 * For the best performance use gst_fft_next_fast_length() to get a
41 * number that is entirely a product of 2, 3 and 5 and use this as the
42 * @len parameter for gst_fft_f32_new().
44 * The @len parameter specifies the number of samples in the time domain that
45 * will be processed or generated. The number of samples in the frequency domain
46 * is @len/2 + 1. To get n samples in the frequency domain use 2*n - 2 as @len.
48 * Before performing the FFT on time domain data it usually makes sense
49 * to apply a window function to it. For this gst_fft_f32_window() can comfortably
52 * Be aware, that you can't simply run gst_fft_f32_inverse_fft() on the
53 * resulting frequency data of gst_fft_f32_fft() to get the original data back.
54 * The relation between them is iFFT (FFT (x)) = x * nfft where nfft is the
55 * length of the FFT. This also has to be taken into account when calculation
56 * the magnitude of the frequency data.
62 * @len: Length of the FFT in the time domain
63 * @inverse: %TRUE if the #GstFFTF32 instance should be used for the inverse FFT
65 * This returns a new #GstFFTF32 instance with the given parameters. It makes
66 * sense to keep one instance for several calls for speed reasons.
68 * @len must be even and to get the best performance a product of
69 * 2, 3 and 5. To get the next number with this characteristics use
70 * gst_fft_next_fast_length().
72 * Returns: a new #GstFFTF32 instance.
75 gst_fft_f32_new (gint len, gboolean inverse)
78 gsize subsize = 0, memneeded;
80 g_return_val_if_fail (len > 0, NULL);
81 g_return_val_if_fail (len % 2 == 0, NULL);
83 kiss_fftr_f32_alloc (len, (inverse) ? 1 : 0, NULL, &subsize);
84 memneeded = ALIGN_STRUCT (sizeof (GstFFTF32)) + subsize;
86 self = (GstFFTF32 *) g_malloc0 (memneeded);
88 self->cfg = (((guint8 *) self) + ALIGN_STRUCT (sizeof (GstFFTF32)));
89 self->cfg = kiss_fftr_f32_alloc (len, (inverse) ? 1 : 0, self->cfg, &subsize);
92 self->inverse = inverse;
100 * @self: #GstFFTF32 instance for this call
101 * @timedata: Buffer of the samples in the time domain
102 * @freqdata: Target buffer for the samples in the frequency domain
104 * This performs the FFT on @timedata and puts the result in @freqdata.
106 * @timedata must have as many samples as specified with the @len parameter while
107 * allocating the #GstFFTF32 instance with gst_fft_f32_new().
109 * @freqdata must be large enough to hold @len/2 + 1 #GstFFTF32Complex frequency
114 gst_fft_f32_fft (GstFFTF32 * self, const gfloat * timedata,
115 GstFFTF32Complex * freqdata)
117 g_return_if_fail (self);
118 g_return_if_fail (!self->inverse);
119 g_return_if_fail (timedata);
120 g_return_if_fail (freqdata);
122 kiss_fftr_f32 (self->cfg, timedata, (kiss_fft_f32_cpx *) freqdata);
126 * gst_fft_f32_inverse_fft:
127 * @self: #GstFFTF32 instance for this call
128 * @freqdata: Buffer of the samples in the frequency domain
129 * @timedata: Target buffer for the samples in the time domain
131 * This performs the inverse FFT on @freqdata and puts the result in @timedata.
133 * @freqdata must have @len/2 + 1 samples, where @len is the parameter specified
134 * while allocating the #GstFFTF32 instance with gst_fft_f32_new().
136 * @timedata must be large enough to hold @len time domain samples.
140 gst_fft_f32_inverse_fft (GstFFTF32 * self, const GstFFTF32Complex * freqdata,
143 g_return_if_fail (self);
144 g_return_if_fail (self->inverse);
145 g_return_if_fail (timedata);
146 g_return_if_fail (freqdata);
148 kiss_fftri_f32 (self->cfg, (kiss_fft_f32_cpx *) freqdata, timedata);
153 * @self: #GstFFTF32 instance for this call
155 * This frees the memory allocated for @self.
159 gst_fft_f32_free (GstFFTF32 * self)
165 * gst_fft_f32_window:
166 * @self: #GstFFTF32 instance for this call
167 * @timedata: Time domain samples
168 * @window: Window function to apply
170 * This calls the window function @window on the @timedata sample buffer.
174 gst_fft_f32_window (GstFFTF32 * self, gfloat * timedata, GstFFTWindow window)
178 g_return_if_fail (self);
179 g_return_if_fail (timedata);
184 case GST_FFT_WINDOW_RECTANGULAR:
187 case GST_FFT_WINDOW_HAMMING:
188 for (i = 0; i < len; i++)
189 timedata[i] *= (0.53836 - 0.46164 * cos (2.0 * G_PI * i / len));
191 case GST_FFT_WINDOW_HANN:
192 for (i = 0; i < len; i++)
193 timedata[i] *= (0.5 - 0.5 * cos (2.0 * G_PI * i / len));
195 case GST_FFT_WINDOW_BARTLETT:
196 for (i = 0; i < len; i++)
197 timedata[i] *= (1.0 - fabs ((2.0 * i - len) / len));
199 case GST_FFT_WINDOW_BLACKMAN:
200 for (i = 0; i < len; i++)
201 timedata[i] *= (0.42 - 0.5 * cos ((2.0 * i) / len) +
202 0.08 * cos ((4.0 * i) / len));
205 g_assert_not_reached ();