CSL 5.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Window.cpp
Go to the documentation of this file.
1 //
2 // Window.cpp -- implementation of the various function window classes
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5
6 #include "Window.h"
7 #include <math.h>
8
9 using namespace csl;
10
11 Window::Window() : UnitGenerator(), mGain(1) {
12 this->setSize(CGestalt::blockSize());
13 }
14
15 // Gain is optional and defaults to 1 when not specified.
16
17 Window::Window(unsigned windowSize, float gain) : UnitGenerator(), mGain(gain) {
18 this->setSize(windowSize);
19 }
20
21 Window::~Window() {
22 mWindowBuffer.freeBuffers();
23 }
24
25 void Window::setGain(float gain) {
26 mGain = gain;
27 fillWindow(); // Re-caluclate window using new gain value.
28 }
29
30 void Window::setSize(unsigned windowSize) {
31 mWindowBufferPos = 0;
32 mWindowSize = windowSize;
33 // If previously allocated, free me first.
34 mWindowBuffer.freeBuffers();
35 mWindowBuffer.setSize(1, mWindowSize); // Allocate memory to store the window.
36 mWindowBuffer.allocateBuffers();
37 fillWindow(); // Fill the buffer with the window data.
38 }
39
40 void Window::nextBuffer(Buffer &outputBuffer, unsigned outBufNum) throw (CException) {
41 /// get everything into registers
42 unsigned numFrames = outputBuffer.mNumFrames;
43 sample* outputBufferPtr = outputBuffer.buffer(outBufNum);
44 sample* windowBufferPtr = mWindowBuffer.buffer(0);
45 unsigned windowBufferPos = mWindowBufferPos;
46 unsigned windowBufferSize = mWindowSize;
47 #ifdef CSL_DEBUG
48 logMsg("Window::nextBuffer");
49 #endif
50 for (unsigned i = 0; i < numFrames; i++) {
51 if (windowBufferPos > windowBufferSize)
52 windowBufferPos = 0;
53 *outputBufferPtr++ = windowBufferPtr[windowBufferPos++];
54 }
55 mWindowBufferPos = windowBufferPos;
56 }
57
58 void Window::fillWindow() {
59 sample* windowBufferPtr = mWindowBuffer.buffer(0);
60 // create the window -- I make a Hamming window, subclasses may override
61 for (unsigned i = 0; i < mWindowSize; i++ )
62 *windowBufferPtr++ = (0.54 - 0.46*cos(CSL_TWOPI*i/(mWindowSize - 1) ));
63 }
64
65 void RectangularWindow::fillWindow() {
66 sample* windowBufferPtr = mWindowBuffer.buffer(0);
67 for (unsigned i = 0; i < mWindowSize; i++ ) // create the window
68 *windowBufferPtr++ = mGain;
69 }
70
71 void TriangularWindow::fillWindow() {
72 sample* windowBufferPtr = mWindowBuffer.buffer(0);
73 float accum = 0.0f;
74 unsigned winHalf = mWindowSize / 2;
75 float step = 1.0f / ((float) winHalf);
76 for (unsigned i = 0; i < winHalf; i++ ) { // create the rising half
77 *windowBufferPtr++ = accum;
78 accum += step;
79 }
80 for (unsigned i = winHalf; i < mWindowSize; i++ ) { // create the falling half
81 *windowBufferPtr++ = accum;
82 accum -= step;
83 }
84 }
85
86 void HammingWindow::fillWindow() {
87 sample* windowBufferPtr = mWindowBuffer.buffer(0);
88 sample increment = CSL_TWOPI/(mWindowSize - 1);
89 for (unsigned i = 0; i < mWindowSize; i++ )
90 *windowBufferPtr++ = (0.54 - 0.46*cos(i * increment));
91 }
92
93 void HannWindow::fillWindow() {
94 sample* windowBufferPtr = mWindowBuffer.buffer(0);
95 sample increment = CSL_TWOPI/(mWindowSize - 1);
96
97 for (unsigned i = 0; i < mWindowSize; i++ )
98 *windowBufferPtr++ = 0.5 * (1 - cos(i * increment)) * mGain;
99 }
100
101 void BlackmanWindow::fillWindow() {
102 sample* windowBufferPtr = mWindowBuffer.buffer(0);
103 sample increment = CSL_TWOPI/(mWindowSize - 1);
104
105 for (unsigned i = 0; i < mWindowSize; i++ )
106 *windowBufferPtr++ = (0.42 - 0.5 * cos(i * increment) + 0.08 * cos(2 * i * increment)) * mGain;
107 }
108
109 void BlackmanHarrisWindow::fillWindow() {
110 sample* windowBufferPtr = mWindowBuffer.buffer(0);
111 sample increment = CSL_TWOPI/(mWindowSize - 1);
112
113 for (unsigned i = 0; i < mWindowSize; i++ )
114 *windowBufferPtr++ = (0.35875 - 0.48829 * cos(i * increment)
115 + 0.14128 * cos(2 * i * increment)
116 - 0.01168 * cos(3 * i * increment)) * mGain;
117 }
118
119 void WelchWindow::fillWindow() {
120 sample* windowBufferPtr = mWindowBuffer.buffer(0);
121 sample increment = 2.f/(mWindowSize - 1);
122 sample phase = -1.f;
123
124 for (unsigned i = 0; i < mWindowSize; i++ ) { // create the window
125 *windowBufferPtr++ = (1.f - phase * phase) * mGain;
126 phase += increment;
127 }
128 }
129
130 void Window::dump() {
131 logMsg("Window of size: %d samples", mWindowSize);
132 }