5aeea68d9cd043184c149b33ca4187423f90116a
[platform/upstream/gstreamer.git] / gst-libs / gst / fft / gstffts16.c
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <math.h>
22
23 #include "_kiss_fft_guts_s16.h"
24 #include "kiss_fftr_s16.h"
25 #include "gstfft.h"
26 #include "gstffts16.h"
27
28 /**
29  * SECTION:gstffts16
30  * @short_description: FFT functions for signed 16 bit integer samples
31  *
32  * #GstFFTS16 provides a FFT implementation and related functions for
33  * signed 16 bit integer samples. To use this call gst_fft_s16_new() for
34  * allocating a #GstFFTS16 instance with the appropriate parameters and
35  * then call gst_fft_s16_fft() or gst_fft_s16_inverse_fft() to perform the
36  * FFT or inverse FFT on a buffer of samples.
37  *
38  * After use free the #GstFFTS16 instance with gst_fft_s16_free().
39  *
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_s16_new().
43  *
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.
47  *
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_s16_window() can comfortably
50  * be used.
51  *
52  * Be aware, that you can't simply run gst_fft_s16_inverse_fft() on the
53  * resulting frequency data of gst_fft_s16_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.
57  *
58  */
59
60 struct _GstFFTS16
61 {
62   void *cfg;
63   gboolean inverse;
64   gint len;
65 };
66
67 /**
68  * gst_fft_s16_new:
69  * @len: Length of the FFT in the time domain
70  * @inverse: %TRUE if the #GstFFTS16 instance should be used for the inverse FFT
71  *
72  * This returns a new #GstFFTS16 instance with the given parameters. It makes
73  * sense to keep one instance for several calls for speed reasons.
74  *
75  * @len must be even and to get the best performance a product of
76  * 2, 3 and 5. To get the next number with this characteristics use
77  * gst_fft_next_fast_length().
78  *
79  * Returns: a new #GstFFTS16 instance.
80  */
81 GstFFTS16 *
82 gst_fft_s16_new (gint len, gboolean inverse)
83 {
84   GstFFTS16 *self;
85   gsize subsize = 0, memneeded;
86
87   g_return_val_if_fail (len > 0, NULL);
88   g_return_val_if_fail (len % 2 == 0, NULL);
89
90   kiss_fftr_s16_alloc (len, (inverse) ? 1 : 0, NULL, &subsize);
91   memneeded = ALIGN_STRUCT (sizeof (GstFFTS16)) + subsize;
92
93   self = (GstFFTS16 *) g_malloc0 (memneeded);
94
95   self->cfg = (((guint8 *) self) + ALIGN_STRUCT (sizeof (GstFFTS16)));
96   self->cfg = kiss_fftr_s16_alloc (len, (inverse) ? 1 : 0, self->cfg, &subsize);
97   g_assert (self->cfg);
98
99   self->inverse = inverse;
100   self->len = len;
101
102   return self;
103 }
104
105 /**
106  * gst_fft_s16_fft:
107  * @self: #GstFFTS16 instance for this call
108  * @timedata: Buffer of the samples in the time domain
109  * @freqdata: Target buffer for the samples in the frequency domain
110  *
111  * This performs the FFT on @timedata and puts the result in @freqdata.
112  *
113  * @timedata must have as many samples as specified with the @len parameter while
114  * allocating the #GstFFTS16 instance with gst_fft_s16_new().
115  *
116  * @freqdata must be large enough to hold @len/2 + 1 #GstFFTS16Complex frequency
117  * domain samples.
118  *
119  */
120 void
121 gst_fft_s16_fft (GstFFTS16 * self, const gint16 * timedata,
122     GstFFTS16Complex * freqdata)
123 {
124   g_return_if_fail (self);
125   g_return_if_fail (!self->inverse);
126   g_return_if_fail (timedata);
127   g_return_if_fail (freqdata);
128
129   kiss_fftr_s16 (self->cfg, timedata, (kiss_fft_s16_cpx *) freqdata);
130 }
131
132 /**
133  * gst_fft_s16_inverse_fft:
134  * @self: #GstFFTS16 instance for this call
135  * @freqdata: Buffer of the samples in the frequency domain
136  * @timedata: Target buffer for the samples in the time domain
137  *
138  * This performs the inverse FFT on @freqdata and puts the result in @timedata.
139  *
140  * @freqdata must have @len/2 + 1 samples, where @len is the parameter specified
141  * while allocating the #GstFFTS16 instance with gst_fft_s16_new().
142  *
143  * @timedata must be large enough to hold @len time domain samples.
144  *
145  */
146 void
147 gst_fft_s16_inverse_fft (GstFFTS16 * self, const GstFFTS16Complex * freqdata,
148     gint16 * timedata)
149 {
150   g_return_if_fail (self);
151   g_return_if_fail (self->inverse);
152   g_return_if_fail (timedata);
153   g_return_if_fail (freqdata);
154
155   kiss_fftri_s16 (self->cfg, (kiss_fft_s16_cpx *) freqdata, timedata);
156 }
157
158 /**
159  * gst_fft_s16_free:
160  * @self: #GstFFTS16 instance for this call
161  *
162  * This frees the memory allocated for @self.
163  *
164  */
165 void
166 gst_fft_s16_free (GstFFTS16 * self)
167 {
168   g_free (self);
169 }
170
171 /**
172  * gst_fft_s16_window:
173  * @self: #GstFFTS16 instance for this call
174  * @timedata: Time domain samples
175  * @window: Window function to apply
176  *
177  * This calls the window function @window on the @timedata sample buffer.
178  *
179  */
180 void
181 gst_fft_s16_window (GstFFTS16 * self, gint16 * timedata, GstFFTWindow window)
182 {
183   gint i, len;
184
185   g_return_if_fail (self);
186   g_return_if_fail (timedata);
187
188   len = self->len;
189
190   switch (window) {
191     case GST_FFT_WINDOW_RECTANGULAR:
192       /* do nothing */
193       break;
194     case GST_FFT_WINDOW_HAMMING:
195       for (i = 0; i < len; i++)
196         timedata[i] *= (0.53836 - 0.46164 * cos (2.0 * G_PI * i / len));
197       break;
198     case GST_FFT_WINDOW_HANN:
199       for (i = 0; i < len; i++)
200         timedata[i] *= (0.5 - 0.5 * cos (2.0 * G_PI * i / len));
201       break;
202     case GST_FFT_WINDOW_BARTLETT:
203       for (i = 0; i < len; i++)
204         timedata[i] *= (1.0 - fabs ((2.0 * i - len) / len));
205       break;
206     case GST_FFT_WINDOW_BLACKMAN:
207       for (i = 0; i < len; i++)
208         timedata[i] *= (0.42 - 0.5 * cos ((2.0 * i) / len) +
209             0.08 * cos ((4.0 * i) / len));
210       break;
211     default:
212       g_assert_not_reached ();
213       break;
214   }
215 }