-
Notifications
You must be signed in to change notification settings - Fork 0
/
HannWindowing.cs
executable file
·112 lines (102 loc) · 3.14 KB
/
HannWindowing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
namespace ffteq
{
class HannWindowing : Windowing
{
private WindowingState state = WindowingState.NoSignal;
private int winLen;
private Signal inSignal;
private Signal outSignal;
private int index;
private bool firstWindow;
/// <summary>
/// Computes value of von Hann function of width winLen in point i.
/// </summary>
private double Hann(int i)
{
double a0 = 0.5;
return a0 - (1 - a0) * Math.Cos(2 * Math.PI * i / winLen);
}
public HannWindowing(int winLen)
{
this.winLen = winLen;
}
public override void StartProcessing(Signal inSignal)
{
if (inSignal.SampleNum < winLen)
{
throw new ApplicationException(String.Format(
"Signal too short for window length of {0} samples",
winLen
));
}
this.inSignal = inSignal;
outSignal = new Signal(inSignal.SampleNum, inSignal.SampleRate);
state = WindowingState.WindowReady;
index = 0;
firstWindow = true;
}
public override Signal NextWindow()
{
switch (state)
{
case WindowingState.NoSignal:
throw new ApplicationException(
"Internal error: Windowing not initialized"
);
case WindowingState.WindowReady:
break;
case WindowingState.AwaitingWindow:
throw new ApplicationException(
"Internal error: Next window not ready"
);
case WindowingState.SignalDone:
return null;
}
Signal window = inSignal.Subsignal(index, winLen);
if (firstWindow)
{
// First winLen / 2 samples of signal shouldn't be put through
// window function
for (int i = winLen / 2; i < winLen; i++)
{
window[i] *= Hann(i);
}
firstWindow = false;
}
else
{
for (int i = 0; i < winLen; i++)
{
window[i] *= Hann(i);
}
}
state = WindowingState.AwaitingWindow;
return window;
}
public override void PutBack(Signal window)
{
for (int i = 0; i < winLen; i++)
{
if (index + i >= outSignal.SampleNum)
{
break;
}
outSignal[index + i] += window[i];
}
index += winLen / 2;
if (index >= inSignal.SampleNum)
{
state = WindowingState.SignalDone;
}
else
{
state = WindowingState.WindowReady;
}
}
public override Signal FinishProcessing()
{
return outSignal;
}
}
}