synthesistransform.cpp 3.38 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
28
29

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

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

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

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

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

41

Mert Burkay Çöteli's avatar
Mert Burkay Çöteli committed
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
void* synthesistransform::IFFT(double* istft_out, double* realpart, double* imagpart) {


    fftw_complex* data, * ifft_output;
    fftw_plan       plan_backward;
    int             i;

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

    int outputcounter = 0;

    plan_backward = fftw_plan_dft_1d(this->windowsize, data, 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;

    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;

                data[i][0] = *(realpart + readIndex);
                data[i][1] = *(imagpart + readIndex);

            }

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

                data[i][0] = 0.0; // *(realpart + readIndex);
                data[i][1] = 0.0; // *(imagpart + readIndex);

            }
            // 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
93
94
95
96
            // 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

            for (i = 0; i < this->windowsize/2; i++)
            {
                istft_out[outputcounter] = (ifft_output[i][0] / window[i]) / (this->windowsize / 2); // / 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;

}