# Inverse the Hanning Window [closed]

I am using the following formula to calculate the Hann values for a set of points: $$w(n)=0.5\left(1-\cos\left(2\pi \frac nN\right)\right)$$

To implement this in java, I am using the following code:

private static void applyHannWindow(final double[] points) {
for (int i = 0; i < points.length; i++) {
points[i] = 0.5 * (1.0 - Math.cos(2.0 * Math.PI * points[i] / points.length));
}
}


To take the inverse, solve for $n$, I can re-use the $w(n)$ . Hence, the equation I come up with is this: $$n = \frac{1}{2\pi}\arccos\big(-2w(n) + 1\big)$$

To implement this in Java, I am using the following code:

private static void reverseHannWindow(final double[] points) {
for (int i = 0; i < points.length; i++) {
points[i] = points.length * (Math.acos(-2.0 * points[i] + 1.0) / (2.0 * Math.PI));
}
}


I wrote a simple test to make sure that after I apply the Hann window, and take it's inverse, that I get the same thing. However, I seem not to.

Given the original array:

final double[] data = new double;
data = 200;
data = 210;
data = 215;
data = 207;
data = 203;


I get the following output:

### applying Hann window
0.0
0.0
0.0
0.9045084971874686
0.9045084971874787
### Reversing Hann window
0.0
0.0
0.0
1.9999999999999862
2.0000000000000138


Not sure what I am doing wrong here in applying the inverse to the Hann window?

• This is just wrong math – not a signal processing question as is! If you re-use the $w$, then the inverse is simply the inverse, i.e. $w_\text{inverse}[n]=\frac 1{w[n]}$. – Marcus Müller Jul 9 '17 at 8:17
• by the way, are you positively sure you want to be re-inventing the wheel here? I don't think Java would be the optimal choice for signal processing of any kind (at least certainly not for me, but I might be thinking too performance and/or embedded-centric), but I'm very optimistic someone's implemented something like window generators before. – Marcus Müller Jul 9 '17 at 10:00
• I think you should take the reciprocal, not inverse. – Atul Ingle Jul 9 '17 at 18:20
• @AtulIngle Might you be able to provide an example below? – angryip Jul 9 '17 at 18:27

I think you misinterpret the term inverse here.

Given a window sequence $w[n]$ of length $L$ with a region of support such that $w[n] \not = 0 ,~~$ for $0 \leq n \leq L-1$ , and a signal $x[n]$ you get the windowed signal $v[n] = w[n]x[n]$ , as each sample of the signal $x[n]$ is multipied by the corresponding sample of the window $w[n]$.

Inverting $v[n]$ means, given the windowed sequence $v[n]$ you obtain the original (unweighted) segment of $x[n]$ that resides in the region of suport range of the window $w[n]$ and that's given by: $$x[n] = \frac{v[n]}{w[n]} ~~,~~ w[n] \not = 0$$

The operation of recovering the original signal segment is as follows: Let the original signal be the Matlab vector:

x = [1 2 3 0 5];   % original signal segment with 5 samples (L=5)


let the window be a Hamming type length of 5 samples:

w = hamming(5)';   % 5 sample Hamming window w = [0.08 0.54 1.0 0.54 0.08]


Hence the windowed signal is:

v = x.*w;          % v = [0.08 1.08 3.0 0.0 0.4] (windowed signal)


Now to recover x[n] from v[n] perform the following:

xrec = v./w;      % xrec = [1.0 2.0 3.0 0.0 5.0]; recovered


The operation $x_{rec}[n] = v[n]/w[n]$ is implemented per sample basis, such that $x_{rec} = v/w$ or $x_{rec} = v/w$ or in general $$x_{rec}[n] = v[n]/w[n]$$

Now however the Hann window has the property that its initial and final samples are zero (in effect reducing the window length by 2) therefore an overlapping implementation is necessary to be able to recover $x[n]$ back from multiple $v[n]$ segments, as follows:

Let the original signal be the Matlab vector:

x = [1 2 3 4 5 4 3 2 1];   % original signal segment with 9 samples (L=9)


let's use a Hann window of length 5:

w = hann(5)';   % 5 sample Hann window w = [0.0 0.5 1.0 0.5 0.0]


First embed a zero to the first and last samples of x[n]:

xe = [0 x 0];   % xe = [0 1 2 3 4 5 4 3 2 1 0] (xe has 11 samples)


Now apply windowing into overlapping segments as such:

v1 = x[1:5].*w;          % v1 = [0.0 0.5 2.0 1.5 0.0] (first segment)
v2 = x[4:8].*w;          % v2 = [0.0 2.0 5.0 2.0 0.0] (second segment)
v3 = x[7:11].*w;         % v3 = [0.0 1.5 2.0 0.5 0.0] (third segment)


(notice the MATLAB indice operation x[i:j] to select portions of the input vector)

Now to recover $x[n]$ from $v_k[n]$ perform the following: First note that each segment begins and ends in zero samples that you should discard, and you should consider only the remainig $L-2$ vetween those.

Then consider also the amount of overlap so that you don't recompute a previously computed input sample, then:

x1_rec = v1[2:4]./w[2:4];      % x1_rec = [1.0 2.0 3.0]; recovered first 3 samples of x[n]

x2_rec = v2[2:4]./w[2:4];      % x2_rec = [4.0 5.0 4.0]; recovered middle 3 samples of x[n]

x3_rec = v3[2:4]./w[2:4];      % x3_rec = [3.0 2.0 1.0]; recovered last 3 samples of x[n]

• hmm not sure i see how i'd get the original segments ( sampled signals ) by using your recommendation, especially since you are excluding 0's. Can you provide more details in your answer that shows taking the hann window and reverting it's effect? – angryip Jul 9 '17 at 17:48
• ok let me put a simple example – Fat32 Jul 9 '17 at 17:53
• I'm extremely sorry, it's a MATLAB (and OCTAVE) syntax that denotes sample by sample multiplication between the elements of two vectors. If you omit the period, the bare * symbol denotes the usual matrix product between two properly sized matrices. – Fat32 Jul 9 '17 at 18:23
• Sure enough! thank you for your help, and holding my hand. I just now have to reproduce this in java – angryip Jul 9 '17 at 18:44
• added the MATLAB example for Hann window – Fat32 Jul 9 '17 at 19:18