Blame gst-libs/gst/fft/gstffts32.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <2007> Sebastian Dröge <slomo@circular-chaos.org>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
Packit 971217
#include <glib.h>
Packit 971217
#include <math.h>
Packit 971217
Packit 971217
#include "_kiss_fft_guts_s32.h"
Packit 971217
#include "kiss_fftr_s32.h"
Packit 971217
#include "gstfft.h"
Packit 971217
#include "gstffts32.h"
Packit 971217
Packit 971217
/**
Packit 971217
 * SECTION:gstffts32
Packit 971217
 * @title: GstFFTS32
Packit 971217
 * @short_description: FFT functions for signed 32 bit integer samples
Packit 971217
 *
Packit 971217
 * #GstFFTS32 provides a FFT implementation and related functions for
Packit 971217
 * signed 32 bit integer samples. To use this call gst_fft_s32_new() for
Packit 971217
 * allocating a #GstFFTS32 instance with the appropriate parameters and
Packit 971217
 * then call gst_fft_s32_fft() or gst_fft_s32_inverse_fft() to perform the
Packit 971217
 * FFT or inverse FFT on a buffer of samples.
Packit 971217
 *
Packit 971217
 * After use free the #GstFFTS32 instance with gst_fft_s32_free().
Packit 971217
 *
Packit 971217
 * For the best performance use gst_fft_next_fast_length() to get a
Packit 971217
 * number that is entirely a product of 2, 3 and 5 and use this as the
Packit 971217
 * @len parameter for gst_fft_s32_new().
Packit 971217
 *
Packit 971217
 * The @len parameter specifies the number of samples in the time domain that
Packit 971217
 * will be processed or generated. The number of samples in the frequency domain
Packit 971217
 * is @len/2 + 1. To get n samples in the frequency domain use 2*n - 2 as @len.
Packit 971217
 *
Packit 971217
 * Before performing the FFT on time domain data it usually makes sense
Packit 971217
 * to apply a window function to it. For this gst_fft_s32_window() can comfortably
Packit 971217
 * be used.
Packit 971217
 *
Packit 971217
 * Be aware, that you can't simply run gst_fft_s32_inverse_fft() on the
Packit 971217
 * resulting frequency data of gst_fft_s32_fft() to get the original data back.
Packit 971217
 * The relation between them is iFFT (FFT (x)) = x / nfft where nfft is the
Packit 971217
 * length of the FFT. This also has to be taken into account when calculation
Packit 971217
 * the magnitude of the frequency data.
Packit 971217
 */
Packit 971217
Packit 971217
struct _GstFFTS32
Packit 971217
{
Packit 971217
  void *cfg;
Packit 971217
  gboolean inverse;
Packit 971217
  gint len;
Packit 971217
};
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_fft_s32_new: (skip)
Packit 971217
 * @len: Length of the FFT in the time domain
Packit 971217
 * @inverse: %TRUE if the #GstFFTS32 instance should be used for the inverse FFT
Packit 971217
 *
Packit 971217
 * This returns a new #GstFFTS32 instance with the given parameters. It makes
Packit 971217
 * sense to keep one instance for several calls for speed reasons.
Packit 971217
 *
Packit 971217
 * @len must be even and to get the best performance a product of
Packit 971217
 * 2, 3 and 5. To get the next number with this characteristics use
Packit 971217
 * gst_fft_next_fast_length().
Packit 971217
 *
Packit 971217
 * Returns: a new #GstFFTS32 instance.
Packit 971217
 */
Packit 971217
GstFFTS32 *
Packit 971217
gst_fft_s32_new (gint len, gboolean inverse)
Packit 971217
{
Packit 971217
  GstFFTS32 *self;
Packit 971217
  gsize subsize = 0, memneeded;
Packit 971217
Packit 971217
  g_return_val_if_fail (len > 0, NULL);
Packit 971217
  g_return_val_if_fail (len % 2 == 0, NULL);
Packit 971217
Packit 971217
  kiss_fftr_s32_alloc (len, (inverse) ? 1 : 0, NULL, &subsize);
Packit 971217
  memneeded = ALIGN_STRUCT (sizeof (GstFFTS32)) + subsize;
Packit 971217
Packit 971217
  self = (GstFFTS32 *) g_malloc0 (memneeded);
Packit 971217
Packit 971217
  self->cfg = (((guint8 *) self) + ALIGN_STRUCT (sizeof (GstFFTS32)));
Packit 971217
  self->cfg = kiss_fftr_s32_alloc (len, (inverse) ? 1 : 0, self->cfg, &subsize);
Packit 971217
  g_assert (self->cfg);
Packit 971217
Packit 971217
  self->inverse = inverse;
Packit 971217
  self->len = len;
Packit 971217
Packit 971217
  return self;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_fft_s32_fft:
Packit 971217
 * @self: #GstFFTS32 instance for this call
Packit 971217
 * @timedata: Buffer of the samples in the time domain
Packit 971217
 * @freqdata: Target buffer for the samples in the frequency domain
Packit 971217
 *
Packit 971217
 * This performs the FFT on @timedata and puts the result in @freqdata.
Packit 971217
 *
Packit 971217
 * @timedata must have as many samples as specified with the @len parameter while
Packit 971217
 * allocating the #GstFFTS32 instance with gst_fft_s32_new().
Packit 971217
 *
Packit 971217
 * @freqdata must be large enough to hold @len/2 + 1 #GstFFTS32Complex frequency
Packit 971217
 * domain samples.
Packit 971217
 *
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_fft_s32_fft (GstFFTS32 * self, const gint32 * timedata,
Packit 971217
    GstFFTS32Complex * freqdata)
Packit 971217
{
Packit 971217
  g_return_if_fail (self);
Packit 971217
  g_return_if_fail (!self->inverse);
Packit 971217
  g_return_if_fail (timedata);
Packit 971217
  g_return_if_fail (freqdata);
Packit 971217
Packit 971217
  kiss_fftr_s32 (self->cfg, timedata, (kiss_fft_s32_cpx *) freqdata);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_fft_s32_inverse_fft:
Packit 971217
 * @self: #GstFFTS32 instance for this call
Packit 971217
 * @freqdata: Buffer of the samples in the frequency domain
Packit 971217
 * @timedata: Target buffer for the samples in the time domain
Packit 971217
 *
Packit 971217
 * This performs the inverse FFT on @freqdata and puts the result in @timedata.
Packit 971217
 *
Packit 971217
 * @freqdata must have @len/2 + 1 samples, where @len is the parameter specified
Packit 971217
 * while allocating the #GstFFTS32 instance with gst_fft_s32_new().
Packit 971217
 *
Packit 971217
 * @timedata must be large enough to hold @len time domain samples.
Packit 971217
 *
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_fft_s32_inverse_fft (GstFFTS32 * self, const GstFFTS32Complex * freqdata,
Packit 971217
    gint32 * timedata)
Packit 971217
{
Packit 971217
  g_return_if_fail (self);
Packit 971217
  g_return_if_fail (self->inverse);
Packit 971217
  g_return_if_fail (timedata);
Packit 971217
  g_return_if_fail (freqdata);
Packit 971217
Packit 971217
  kiss_fftri_s32 (self->cfg, (kiss_fft_s32_cpx *) freqdata, timedata);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_fft_s32_free:
Packit 971217
 * @self: #GstFFTS32 instance for this call
Packit 971217
 *
Packit 971217
 * This frees the memory allocated for @self.
Packit 971217
 *
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_fft_s32_free (GstFFTS32 * self)
Packit 971217
{
Packit 971217
  g_free (self);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_fft_s32_window:
Packit 971217
 * @self: #GstFFTS32 instance for this call
Packit 971217
 * @timedata: Time domain samples
Packit 971217
 * @window: Window function to apply
Packit 971217
 *
Packit 971217
 * This calls the window function @window on the @timedata sample buffer.
Packit 971217
 *
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_fft_s32_window (GstFFTS32 * self, gint32 * timedata, GstFFTWindow window)
Packit 971217
{
Packit 971217
  gint i, len;
Packit 971217
Packit 971217
  g_return_if_fail (self);
Packit 971217
  g_return_if_fail (timedata);
Packit 971217
Packit 971217
  len = self->len;
Packit 971217
Packit 971217
  switch (window) {
Packit 971217
    case GST_FFT_WINDOW_RECTANGULAR:
Packit 971217
      /* do nothing */
Packit 971217
      break;
Packit 971217
    case GST_FFT_WINDOW_HAMMING:
Packit 971217
      for (i = 0; i < len; i++)
Packit 971217
        timedata[i] *= (0.53836 - 0.46164 * cos (2.0 * G_PI * i / len));
Packit 971217
      break;
Packit 971217
    case GST_FFT_WINDOW_HANN:
Packit 971217
      for (i = 0; i < len; i++)
Packit 971217
        timedata[i] *= (0.5 - 0.5 * cos (2.0 * G_PI * i / len));
Packit 971217
      break;
Packit 971217
    case GST_FFT_WINDOW_BARTLETT:
Packit 971217
      for (i = 0; i < len; i++)
Packit 971217
        timedata[i] *= (1.0 - fabs ((2.0 * i - len) / len));
Packit 971217
      break;
Packit 971217
    case GST_FFT_WINDOW_BLACKMAN:
Packit 971217
      for (i = 0; i < len; i++)
Packit 971217
        timedata[i] *= (0.42 - 0.5 * cos ((2.0 * i) / len) +
Packit 971217
            0.08 * cos ((4.0 * i) / len));
Packit 971217
      break;
Packit 971217
    default:
Packit 971217
      g_assert_not_reached ();
Packit 971217
      break;
Packit 971217
  }
Packit 971217
}