analysistransform.cpp 6.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include "analysistransform.h"



// Create a constructor for analysis transform
analysistransform::analysistransform()
{
    this->signallength = 20480;
    this->windowsize = 2048;
    this->overlap = 1024;
    this->nofmics = 32;
12
13
14
15
16

    // Create a hamming window of appropriate length
    window = (double*)malloc(sizeof(double) * this->windowsize);

    hamming(this->windowsize, window);
17
18
19
}

// Create a constructor for analysis transform
20
analysistransform::analysistransform(int windowsize, int signallength, int hopsize,int nofmics)
21
22
23
24
25
{
    this->signallength = signallength;
    this->windowsize = windowsize;
    this->overlap = hopsize;
    this->nofmics = nofmics;
26
27

    // Create a hamming window of appropriate length
28
    window = (double*)malloc(sizeof(double) * (this->windowsize));
29
30

    hamming(this->windowsize, window);
31
32
33
}

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

36
    for (int i = 0; i <= windowLength; i++) {
37

38
        buffer[i] = (0.53836 - (0.46164 * cos(2 * PI * ((double)i / (((double)windowLength) * 1.0)))));
39
40
41
42
43
    }
}

/*
Signal in : x[0] to x[signalLength - 1]
44
nofmics : the number of the microphones
45
46
47
realpart : fft_real[0] to fft_real[N/2 -1]
imagpart : imagpart[0] to imagpart[N/2 -1]
*/
48
void* analysistransform::FFT(double* signal, double* realpart, double* imagpart) {
49
50
51
52


    double* data;
    fftw_complex* fft_result;
53
    fftw_plan       plan_forward;
54
55
56
57
    int             i;

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

58
    fft_result = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * this->windowsize);
59
60
61
62
63
64
65
66

    plan_forward = fftw_plan_dft_r2c_1d(this->windowsize, data, fft_result, FFTW_ESTIMATE);

    int readIndex;

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

67
    int channelindex = 0;
68

69
    // Process each chunk of the signal
70

71
72
73
74
    for (channelindex = 0; channelindex < this->nofmics; channelindex++)
    {
        int outputcounter = channelindex;
        int chunkPosition = 0;
75

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

78
79
            // Copy the chunk into our buffer
            for (i = 0; i < this->windowsize; i++) {
80
81


82
                readIndex = (chunkPosition + i) * this->nofmics + channelindex;
83

84
                if (readIndex < this->signallength * this->nofmics) {
85

86
                    // Note the windowing!
87
                    data[i] = signal[readIndex] * window[i+1];
88

89
90
                }
                else  {
91

92
                    // we have read beyond the signal, so zero-pad it!
93

94
                    data[i] = 0.0;
95

96
                    bStop = 1;
97

98
                }
99
100
            }

101
102
            // Perform the FFT on our chunk
            fftw_execute(plan_forward);
103
104


105
106
107
108
            // 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.
109
110


111
            for (i = 0; i <= this->windowsize / 2; i++) {
112

113
114
                *(realpart + outputcounter) = fft_result[i][0];
                *(imagpart + outputcounter) = fft_result[i][1];
115

116
117
                outputcounter += this->nofmics;
            }
118
119


120
            chunkPosition += this->overlap;
121

122
123
        } // Excuse the formatting, the while ends here.
        bStop = 0;
124

125
    }
126
127
128
129
130
131
132
133
    fftw_destroy_plan(plan_forward);

    fftw_free(data);
    fftw_free(fft_result);

    return 0;

}
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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228

/*
Signal in : x[0] to x[signalLength - 1]
nofmics : the number of the microphones
realpart : fft_real[0] to fft_real[N/2 -1]
imagpart : imagpart[0] to imagpart[N/2 -1]
*/
void* analysistransform::FFT(double* signal, float* realpart, float* imagpart) {


    double* data;
    fftw_complex* fft_result;
    fftw_plan       plan_forward;
    int             i;

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

    fft_result = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * this->windowsize );

    plan_forward = fftw_plan_dft_r2c_1d(this->windowsize, data, fft_result, FFTW_ESTIMATE);

    int readIndex;

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

    int channelindex = 0;

    // Process each chunk of the signal

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

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

            // Copy the chunk into our buffer
            for (i = 0; i < this->windowsize; i++) {


                readIndex = (chunkPosition + i) * this->nofmics + channelindex;

                if (readIndex < this->signallength * this->nofmics) {

                    // Note the windowing!
                    data[i] = signal[readIndex]; //   *window[i];

                }
                else {

                    // we have read beyond the signal, so zero-pad it!

                    data[i] = 0.0;

                    bStop = 1;

                }
            }

            // Perform the FFT on our chunk
            fftw_execute(plan_forward);


            // 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.


            for (i = 0; i < this->windowsize / 2; i++) {
//USE THIS ONE !/////////////////////////////////////////////////////////////////////////////////////////
               *(realpart + outputcounter) = (float)fft_result[i][0] / (float)((this->windowsize));
              *(imagpart + outputcounter) = (float)fft_result[i][1] / (float)((this->windowsize));
//USE THIS ONE !/////////////////////////////////////////////////////////////////////////////////////////
              //  *(realpart + outputcounter) = ((float)fft_result[i][0] * window[i]) / (float)((this->windowsize) / 2.0);
              //  *(imagpart + outputcounter) = ((float)fft_result[i][1] * window[i]) / (float)((this->windowsize) / 2.0);
                outputcounter += this->nofmics;
            }


            chunkPosition += this->overlap;

        } // Excuse the formatting, the while ends here.
        bStop = 0;

    }
    fftw_destroy_plan(plan_forward);

    fftw_free(data);
    fftw_free(fft_result);

    return 0;

}