analysistransform.cpp 3.45 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
28
29
30

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

    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
37

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

38
        buffer[i] = 0.54 - (0.46 * cos(2 * PI * (i / (((double)windowLength - 1.0) * 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
87
                    // Note the windowing!
                    data[i] = signal[readIndex] * window[i];
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;

}