synthesistransform.cpp 8.33 KB
Newer Older
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
1
2
3
4
5
#include "synthesistransform.h"




6
// Create a constructor for analysis transform
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
7
8
9
10
11
12
synthesistransform::synthesistransform()
{
    this->signallength = 20480;
    this->windowsize = 2048;
    this->overlap = 1024;
    this->nofmics = 32;
13
14
15
16

    window = (double*)malloc(sizeof(double) * this->windowsize);

    hamming(this->windowsize, window);
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
17
18
}

19
// Create a constructor for analysis transform
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
20
21
22
23
24
25
synthesistransform::synthesistransform(int windowsize, int signallength, int hopsize, int nofmics)
{
    this->signallength = signallength;
    this->windowsize = windowsize;
    this->overlap = hopsize;
    this->nofmics = nofmics;
26

27
    window = (double*)malloc(sizeof(double) * (this->windowsize+1));
28
29

    hamming(this->windowsize, window);
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
30
31
32
33
34
}

// Create a hamming window of windowLength samples in buffer
void synthesistransform::hamming(int windowLength, double* buffer) {

35
    for (int i = 0; i <= windowLength; i++) {
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
36

37
        buffer[i] = (0.53836 - (0.46164 * cos(2 * PI * ((double)i / (((double)windowLength) * 1.0))))) ;
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
38
39
40
    }
}

41
42
43
44
45
46
47
void synthesistransform::SetSourceSize(int nofsource)
{

    this->nofmics = nofsource;

}

48

Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
49
50
51
void* synthesistransform::IFFT(double* istft_out, double* realpart, double* imagpart) {


52
    complex<double>* data, * ifft_output;
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
53
54
55
    fftw_plan       plan_backward;
    int             i;

56
57
    data = (complex<double>*)fftw_malloc(sizeof(complex<double>) * this->windowsize);
    ifft_output = (complex<double>*)fftw_malloc(sizeof(complex<double>) * this->windowsize);
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
58
59
60

    int outputcounter = 0;

61
    plan_backward = fftw_plan_dft_1d(this->windowsize, reinterpret_cast<fftw_complex*>(data), reinterpret_cast<fftw_complex*>(ifft_output), FFTW_BACKWARD, FFTW_ESTIMATE);
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
62
63
64
65
66
67
68
69
70

    int readIndex;

    // Should we stop reading in chunks?
    int bStop = 0;

    // Process each chunk of the signal
    int channelindex = 0;

71

Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
72
73
74
75
76
77
78
79
80
81
82
    for (channelindex = 0; channelindex < this->nofmics; channelindex++)
    {
        int outputcounter = channelindex;
        int chunkPosition = 0;

        while (chunkPosition < this->signallength && !bStop) {

            for (i = 0; i < this->windowsize / 2; i++)
            {
                readIndex = (chunkPosition + i) * this->nofmics + channelindex;

83
84
                reinterpret_cast<fftw_complex*>(data)[i][0] = *(realpart + readIndex);
                reinterpret_cast<fftw_complex*>(data)[i][1] = *(imagpart + readIndex); 
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
85
86
87
88
89

            }

            for (i = this->windowsize / 2; i < this->windowsize; i++)
            {
90
                //readIndex = (chunkPosition + (i - this->windowsize / 2)) * this->nofmics + channelindex;
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
91

92
93
                reinterpret_cast<fftw_complex*>(data)[i][0] = 0.0; // reinterpret_cast<fftw_complex*>(data)[this->windowsize - i - 1][0]; // *(realpart + readIndex);
                reinterpret_cast<fftw_complex*>(data)[i][1] = 0.0; // reinterpret_cast<fftw_complex*>(data)[this->windowsize - i - 1][1]; // *(imagpart + readIndex);
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
94
95
96
97
98
99
100

            }
            // Perform the FFT on our chunk
            fftw_execute(plan_backward);

            // Copy the first (windowSize/2 + 1) data points into your spectrogram.
            // We do this because the FFT output is mirrored about the nyquist
101
102
103
104
            // frequency, so the second half of the data is redundant. This is how
            // Matlab's spectrogram routine works.

            //outputcounter = chunkPosition * this->nofmics + channelindex;
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
105
106
107

            for (i = 0; i < this->windowsize/2; i++)
            {
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
                 
                istft_out[outputcounter] = (double)(ifft_output[i].real());/* / (double)window[i]*/; // / (double)((this->windowsize / 2.0)); // / windowSize; // The i term should be non-existent.

                outputcounter += this->nofmics;
            }

            chunkPosition += this->overlap;

        } // Excuse the formatting, the while ends here.

        bStop = 0;
    }

    fftw_destroy_plan(plan_backward);

    fftw_free(data);
    fftw_free(ifft_output);
    return 0;

}

void* synthesistransform::IFFT(double* istft_out, float* realpart, float* imagpart) {


    complex<double>* data, * ifft_output;
    fftw_plan       plan_backward;
    int             i;

    data = (complex<double>*)fftw_malloc(sizeof(complex<double>) * this->windowsize);
    ifft_output = (complex<double>*)fftw_malloc(sizeof(complex<double>) * this->windowsize);

    int outputcounter = 0;

    plan_backward = fftw_plan_dft_1d(this->windowsize, reinterpret_cast<fftw_complex*>(data), reinterpret_cast<fftw_complex*>(ifft_output), FFTW_BACKWARD, FFTW_ESTIMATE);

    int readIndex;

    // Should we stop reading in chunks?
    int bStop = 0;

    // Process each chunk of the signal
    int channelindex = 0;
    int zeropadding = 0;

    for (channelindex = 0; channelindex < this->nofmics; channelindex++)
    {
        int outputcounter = channelindex;
        int chunkPosition = 0;

       /* for (int ki = 0; ki < (this->signallength * this->nofmics); ki++)
        {
            istft_out[ki] = 0.0;
        }*/

        //float pressurelev = 0.00001 / pressurelevel[channelindex];

        while (chunkPosition < this->signallength && !bStop) {

            for (i = 0; i < zeropadding; i++)
            {
                //readIndex = (chunkPosition + (i - this->windowsize / 2)) * this->nofmics + channelindex;

                reinterpret_cast<fftw_complex*>(data)[i][0] = 0.0; // reinterpret_cast<fftw_complex*>(data)[this->windowsize - i - 1][0]; // *(realpart + readIndex);
                reinterpret_cast<fftw_complex*>(data)[i][1] = 0.0; // reinterpret_cast<fftw_complex*>(data)[this->windowsize - i - 1][1]; // *(imagpart + readIndex);
            }
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
            for (i = zeropadding; i < this->windowsize / 2; i++)
            {
                readIndex = chunkPosition  * this->nofmics + i * this->nofmics + channelindex;
                // If there is not an analysis use directly
                reinterpret_cast<fftw_complex*>(data)[i][0] = *(realpart + readIndex); // *window[i]; // *(float)((this->windowsize));
                reinterpret_cast<fftw_complex*>(data)[i][1] = *(imagpart + readIndex); // *window[i]; // *(float)((this->windowsize));
                
//USE THIS ONE !/////////////////////////////////////////////////////////////////////////////////////////
              //reinterpret_cast<fftw_complex*>(data)[i][0] = *(realpart + readIndex);
              //reinterpret_cast<fftw_complex*>(data)[i][1] = *(imagpart + readIndex);
//USE THIS ONE !/////////////////////////////////////////////////////////////////////////////////////////

            }
            for (i = (this->windowsize / 2); i < this->windowsize; i++)
            {
                readIndex = (chunkPosition + (this->windowsize -i - 1)) * this->nofmics + channelindex;

                reinterpret_cast<fftw_complex*>(data)[i][0] = 0.0; // *(realpart + readIndex)* window[(this->windowsize / 2) - i];
                reinterpret_cast<fftw_complex*>(data)[i][1] = 0.0; //  *(imagpart + readIndex)* (-1) * window[this->windowsize - i];

            }
            // Perform the FFT on our chunk
            fftw_execute(plan_backward);

            // Copy the first (windowSize/2 + 1) data points into your spectrogram.
            // We do this because the FFT output is mirrored about the nyquist
            // frequency, so the second half of the data is redundant. This is how
            // Matlab's spectrogram routine works.

            //outputcounter = chunkPosition * this->nofmics + channelindex;


            
            for (i = 0; i < this->windowsize  ; i++)
            {
                // If there is not an analysis use directly
                istft_out[outputcounter] = (double)(ifft_output[i].real()); // / windowSize; // The i term should be non-existent.
//USE THIS ONE !/////////////////////////////////////////////////////////////////////////////////////////
                // Without analysis use this one:
              //  istft_out[outputcounter] = ((double)(ifft_output[i].real()) / window[i]); // / (double)((this->windowsize));
//USE THIS ONE !/////////////////////////////////////////////////////////////////////////////////////////
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
215
216
217
218
                outputcounter += this->nofmics;
            }

            chunkPosition += this->overlap;
219
            outputcounter -= this->overlap * this->nofmics;
Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
220
221
222
223
224
225
226
227
228
229
230
231
232

        } // Excuse the formatting, the while ends here.

        bStop = 0;
    }

    fftw_destroy_plan(plan_backward);

    fftw_free(data);
    fftw_free(ifft_output);
    return 0;

}