]> git.sesse.net Git - kdenlive/blob - src/audioscopes/ffttools.cpp
Audio Spectrum: Different Window functions added (Rectangle, Triangle, and Hamming)
[kdenlive] / src / audioscopes / ffttools.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10
11 #include <math.h>
12
13 #include "ffttools.h"
14
15 //#define DEBUG_FFTTOOLS
16 #ifdef DEBUG_FFTTOOLS
17 #include <QDebug>
18 #endif
19
20 FFTTools::FFTTools()
21 {
22 }
23
24 // http://cplusplus.syntaxerrors.info/index.php?title=Cannot_declare_member_function_%E2%80%98static_int_Foo::bar%28%29%E2%80%99_to_have_static_linkage
25 const QVector<float> FFTTools::window(WindowType windowType, const int size, const float param)
26 {
27     // Deliberately avoid converting size to a float
28     // to keep mid an integer.
29     float mid = (size-1)/2;
30     float max = size-1;
31     QVector<float> window;
32
33     switch (windowType) {
34     case Window_Rect:
35         return QVector<float>(size+1, 1);
36         break;
37     case Window_Triangle:
38         window = QVector<float>(size+1);
39
40         for (int x = 0; x < mid; x++) {
41             window[x] = x/mid + (mid-x)/mid*param;
42         }
43         for (int x = mid; x < size; x++) {
44             window[x] = (x-mid)/(max-mid) * param + (max-x)/(max-mid);
45         }
46         window[size] = .5 + param/2;
47
48 #ifdef DEBUG_FFTTOOLS
49         qDebug() << "Triangle window (factor " << window[size] << "):";
50         for (int i = 0; i < size; i++) {
51             qDebug() << window[i];
52         }
53         qDebug() << "Triangle window end.";
54 #endif
55
56         return window;
57         break;
58     case Window_Hamming:
59         // Use a quick version of the Hamming window here: Instead of
60         // interpolating values between (-max/2) and (max/2)
61         // we use integer values instead, ranging from -mid to (max-mid).
62         window = QVector<float>(size+1);
63
64         for (int x = 0; x < size; x++) {
65             window[x] = .54 + .46 * cos( 2*M_PI*(x-mid) / size );
66         }
67
68         // Integrating the cosine over the window function results in
69         // an area of 0; So only the constant factor 0.54 counts.
70         window[size] = .54;
71
72 #ifdef DEBUG_FFTTOOLS
73         qDebug() << "Hanning window (factor " << window[size] << "):";
74         for (int i = 0; i < size; i++) {
75             qDebug() << window[i];
76         }
77         qDebug() << "Hanning window end.";
78 #endif
79
80         return window;
81         break;
82     }
83     Q_ASSERT(false);
84     return QVector<float>();
85 }
86
87 #ifdef DEBUG_FFTTOOLS
88 #undef DEBUG_FFTTOOLS
89 #endif