samer@0
|
1 /*
|
samer@0
|
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
3 * All rights reserved.
|
samer@0
|
4 *
|
samer@0
|
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
6 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
7 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
8 */
|
samer@0
|
9
|
samer@0
|
10 package samer.units;
|
samer@0
|
11
|
samer@0
|
12 import samer.maths.*;
|
samer@0
|
13 import samer.core.*;
|
samer@0
|
14 import samer.core.types.*;
|
samer@0
|
15 import samer.tools.*;
|
samer@0
|
16 import samer.functions.*;
|
samer@0
|
17 import javax.sound.sampled.*;
|
samer@0
|
18
|
samer@0
|
19 /**
|
samer@0
|
20 * Overlap and add input vectors into a buffer. At each iteration,
|
samer@0
|
21 * the buffer is shifted by <hop> and the new data is added in.
|
samer@0
|
22 */
|
samer@0
|
23
|
samer@0
|
24 public class OverlapAndAdd extends NamedTask
|
samer@0
|
25 {
|
samer@0
|
26 VVector vec; // input
|
samer@0
|
27 VVector out;
|
samer@0
|
28 int N, hop; // frame size, hop size
|
samer@0
|
29 double[] x, y; // input and output arrays
|
samer@0
|
30 double[] H; // array containing window, eg Hanning
|
samer@0
|
31
|
samer@0
|
32 /** Create overlap and add buffer for input vector with given hop size */
|
samer@0
|
33
|
samer@0
|
34 public OverlapAndAdd(VVector input, int hop) throws Exception
|
samer@0
|
35 {
|
samer@0
|
36 super("overlap");
|
samer@0
|
37 vec = input; N = vec.size();
|
samer@0
|
38 out = new VVector("output",N);
|
samer@0
|
39 H=new double[N];
|
samer@0
|
40 x=vec.array();
|
samer@0
|
41 y=out.array();
|
samer@0
|
42 this.hop=hop;
|
samer@0
|
43 }
|
samer@0
|
44
|
samer@0
|
45 /** Sets windowing array to samples from the given function.
|
samer@0
|
46 * The domain [0,1) is mapped on to the array indices 0:N-1
|
samer@0
|
47 */
|
samer@0
|
48
|
samer@0
|
49 public void setWindow(Function fn) {
|
samer@0
|
50 for (int i=0; i<N; i++) H[i] = (double)i/N;
|
samer@0
|
51 fn.apply(H);
|
samer@0
|
52 }
|
samer@0
|
53
|
samer@0
|
54 public VVector input() { return vec; }
|
samer@0
|
55 public VVector output() { return out; }
|
samer@0
|
56 public void setHop(int hop) { this.hop=hop; }
|
samer@0
|
57
|
samer@0
|
58 public void dispose() { out.dispose(); }
|
samer@0
|
59 public void run() throws Exception
|
samer@0
|
60 {
|
samer@0
|
61 // shift everything in buffer along
|
samer@0
|
62 System.arraycopy(y,hop,y,0,N-hop);
|
samer@0
|
63
|
samer@0
|
64 // addin new stuff
|
samer@0
|
65 int i=0;
|
samer@0
|
66 for (; i<N-hop; i++) y[i]+=H[i]*x[i];
|
samer@0
|
67 for (; i<N; i++) y[i]=H[i]*x[i];
|
samer@0
|
68
|
samer@0
|
69 out.changed();
|
samer@0
|
70 }
|
samer@0
|
71 }
|
samer@0
|
72
|