summaryrefslogtreecommitdiff
path: root/attic/muse_qt4_evolution/synti/zynaddsubfx/DSP/FFTwrapper.C
blob: 7c67e6319b5e7980b77a98ee1c83666c78570101 (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
/*
  ZynAddSubFX - a software synthesizer
 
  FFTwrapper.c  -  A wrapper for Fast Fourier Transforms
  Copyright (C) 2002-2005 Nasca Octavian Paul
  Author: Nasca Octavian Paul

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License 
  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 (version 2) for more details.

  You should have received a copy of the GNU General Public License (version 2)
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/

#include <math.h>
#include "FFTwrapper.h"

FFTwrapper::FFTwrapper(int fftsize_){
    fftsize=fftsize_;
    tmpfftdata1=new fftw_real[fftsize];
    tmpfftdata2=new fftw_real[fftsize];
#ifdef FFTW_VERSION_2
    planfftw=rfftw_create_plan(fftsize,FFTW_REAL_TO_COMPLEX,FFTW_ESTIMATE|FFTW_IN_PLACE);
    planfftw_inv=rfftw_create_plan(fftsize,FFTW_COMPLEX_TO_REAL,FFTW_ESTIMATE|FFTW_IN_PLACE);
#else
    planfftw=fftw_plan_r2r_1d(fftsize,tmpfftdata1,tmpfftdata1,FFTW_R2HC,FFTW_ESTIMATE);
    planfftw_inv=fftw_plan_r2r_1d(fftsize,tmpfftdata2,tmpfftdata2,FFTW_HC2R,FFTW_ESTIMATE);
#endif
};

FFTwrapper::~FFTwrapper(){
#ifdef FFTW_VERSION_2
    rfftw_destroy_plan(planfftw);
    rfftw_destroy_plan(planfftw_inv);
#else 
    fftw_destroy_plan(planfftw);
    fftw_destroy_plan(planfftw_inv);
#endif

    delete [] tmpfftdata1;
    delete [] tmpfftdata2;
};

/*
 * do the Fast Fourier Transform
 */
void FFTwrapper::smps2freqs(REALTYPE *smps,FFTFREQS freqs){
#ifdef FFTW_VERSION_2
    for (int i=0;i<fftsize;i++) tmpfftdata1[i]=smps[i];
    rfftw_one(planfftw,tmpfftdata1,tmpfftdata2);
    for (int i=0;i<fftsize/2;i++) {
	freqs.c[i]=tmpfftdata2[i];
	if (i!=0) freqs.s[i]=tmpfftdata2[fftsize-i];
    };
#else
    for (int i=0;i<fftsize;i++) tmpfftdata1[i]=smps[i];
    fftw_execute(planfftw);
    for (int i=0;i<fftsize/2;i++) {
	freqs.c[i]=tmpfftdata1[i];
	if (i!=0) freqs.s[i]=tmpfftdata1[fftsize-i];
    };
#endif
    tmpfftdata2[fftsize/2]=0.0;
};

/*
 * do the Inverse Fast Fourier Transform
 */
void FFTwrapper::freqs2smps(FFTFREQS freqs,REALTYPE *smps){
    tmpfftdata2[fftsize/2]=0.0;
#ifdef FFTW_VERSION_2
    for (int i=0;i<fftsize/2;i++) {
	tmpfftdata1[i]=freqs.c[i];
	if (i!=0) tmpfftdata1[fftsize-i]=freqs.s[i];
    };
    rfftw_one(planfftw_inv,tmpfftdata1,tmpfftdata2);
    for (int i=0;i<fftsize;i++) smps[i]=tmpfftdata2[i];
#else
    for (int i=0;i<fftsize/2;i++) {
	tmpfftdata2[i]=freqs.c[i];
	if (i!=0) tmpfftdata2[fftsize-i]=freqs.s[i];
    };
    fftw_execute(planfftw_inv);
    for (int i=0;i<fftsize;i++) smps[i]=tmpfftdata2[i];
#endif

};