summaryrefslogtreecommitdiff
path: root/gr-vocoder/lib/codec2/fft.c
blob: a33e4d2c8a4d1cc861ea92de2e1b8d4beff1b40b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*---------------------------------------------------------------------------*\
                                                 
  FILE........: fft.c                                                  
  AUTHOR......: Bruce Robertson                                      
  DATE CREATED: 20/11/2010                            
                                                         
  Bridging function to the kiss_fft package.      
                                                               
\*---------------------------------------------------------------------------*/

/*
  Copyright (C) 2010 Bruce Robertson

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include "kiss_fft.h"

/*---------------------------------------------------------------------------*\
                                                                            
                                GLOBALS                                       
                                                                             
\*---------------------------------------------------------------------------*/

kiss_fft_cpx *fin;
kiss_fft_cpx *fout;
kiss_fft_cfg cfg_forward;
kiss_fft_cfg cfg_reverse;

/*---------------------------------------------------------------------------*\
                                                                             
  initialize_fft(int n)                                                                  
                                                                             
  Initialisation function for kiss_fft. This assumes that all calls to fft() 
  use the same datatypes and are one arrays of the same size.

\*---------------------------------------------------------------------------*/

void
initialize_fft (int n)
{
  fin = KISS_FFT_MALLOC (n * sizeof (kiss_fft_cpx));
  assert(fin != NULL);
  fout = KISS_FFT_MALLOC (n * sizeof (kiss_fft_cpx));
  assert(fout != NULL);
  cfg_forward = kiss_fft_alloc (n, 0, NULL, NULL);
  assert(cfg_forward != NULL);
  cfg_reverse = kiss_fft_alloc (n, 1, NULL, NULL);
  assert(cfg_reverse != NULL);
}

/*---------------------------------------------------------------------------*\
                                                                             
  fft(float x[], int n, int isign)                                                
  Function that calls kiss_fft with the signature of four1 from NRC.

\*---------------------------------------------------------------------------*/


void
fft (float x[], int n, int isign)
{
  int isReverse = 0;
  int c;
  kiss_fft_cfg cfg;
  if (cfg_forward == NULL)
    {
      initialize_fft (n);
    }
  for (c = 0; c < n * 2; c += 2)
    {
      fin[c / 2].r = x[c];
      fin[c / 2].i = -x[c + 1];
    }
  if (isign == -1)
    {
      cfg = cfg_reverse;
    }
  else
    {
      cfg = cfg_forward;
    }
  kiss_fft (cfg, fin, fout);
  for (c = 0; c < n * 2; c += 2)
    {
      x[c] = fout[(c) / 2].r;
      x[c + 1] = -fout[(c) / 2].i;
    }
}