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



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

// Create a constructor for analysis transform
15
analysistransform::analysistransform(int windowsize, int signallength, int hopsize,int nofmics)
16
17
18
19
20
21
22
23
{
    this->signallength = signallength;
    this->windowsize = windowsize;
    this->overlap = hopsize;
    this->nofmics = nofmics;
}

// Create a hamming window of windowLength samples in buffer
24
void analysistransform::hamming(int windowLength, double* buffer) {
25
26
27
28
29
30
31
32
33

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

        buffer[i] = 0.54 - (0.46 * cos(2 * PI * (i / ((windowLength - 1) * 1.0))));
    }
}

/*
Signal in : x[0] to x[signalLength - 1]
34
nofmics : the number of the microphones
35
36
37
realpart : fft_real[0] to fft_real[N/2 -1]
imagpart : imagpart[0] to imagpart[N/2 -1]
*/
38
void* analysistransform::FFT(double* signal, double* realpart, double* imagpart) {
39
40
41
42


    double* data;
    fftw_complex* fft_result;
43
    fftw_plan       plan_forward;
44
45
46
47
    int             i;

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

48
    fft_result = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * this->windowsize);
49
50
51
52

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

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

    hamming(this->windowsize, window);

57
    
58
59
60
61
62
63

    int readIndex;

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

64
    int channelindex = 0;
65

66
    // Process each chunk of the signal
67

68
69
70
71
    for (channelindex = 0; channelindex < this->nofmics; channelindex++)
    {
        int outputcounter = channelindex;
        int chunkPosition = 0;
72

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

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


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

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

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

86
87
                }
                else  {
88

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

91
                    data[i] = 0.0;
92

93
                    bStop = 1;
94

95
                }
96
97
            }

98
99
            // Perform the FFT on our chunk
            fftw_execute(plan_forward);
100
101


102
103
104
105
            // 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.
106
107


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

110
111
                *(realpart + outputcounter) = fft_result[i][0];
                *(imagpart + outputcounter) = fft_result[i][1];
112

113
114
                outputcounter += this->nofmics;
            }
115
116


117
            chunkPosition += this->overlap;
118

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

122
    }
123
124
125
126
127
128
129
130
131
    fftw_destroy_plan(plan_forward);

    fftw_free(data);
    fftw_free(fft_result);
    free(window);

    return 0;

}