File: KJFFT.java

package info (click to toggle)
libkjdsp-java 1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 80 kB
  • ctags: 134
  • sloc: java: 618; xml: 21; sh: 14; makefile: 11
file content (200 lines) | stat: -rw-r--r-- 5,117 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package kj.dsp;

/**
 * @author Kris
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class KJFFT {

    private float[] xre;
    private float[] xim;
    private float[] mag;
    
    private float[] fftSin;
    private float[] fftCos;
    private int[]   fftBr;
    
    private int ss, ss2, nu, nu1;
    
	/**
	 * @param The amount of the sample provided to the "calculate" method to use during
	 *        FFT calculations.
	 */
	public KJFFT( int pSampleSize ) {
        
		ss  = pSampleSize;
		ss2 = ss >> 1;
			
		xre = new float[ ss ];
        xim = new float[ ss ];
        mag = new float[ ss2 ];
		
        nu = (int)( Math.log( ss ) / Math.log( 2 ) );
        nu1 = nu - 1;
        
        prepareFFTTables();
        
	}

    private int bitrev( int j, int nu ) {

        int j1 = j;
        int j2;
        int k = 0;
        
        for( int i = 1; i <= nu; i++ ) {
            j2 = j1 >> 1;
            k  = ( k << 1 ) + j1 - ( j2 << 1 );
            j1 = j2;
        }
        
        return k;
        
    }

    
    /**
     * @param  pSample The sample to compute FFT values on.
     * @return         The results of the calculation, normalized between 0.0 and 1.0. 
     */
    public float[] calculate( float[] pSample ) {
        
        int n2  = ss2;
        int nu1 = nu - 1;
        
        int wAps = pSample.length / ss;
        
        // -- FIXME: This affects the calculation accuracy, because
        //           is compresses the digital signal. Looks nice on
        //           the spectrum analyser, as it chops off most of 
        //           sound we cannot hear anyway.
        for ( int a = 0, b = 0; a < pSample.length; a += wAps, b++ ) {
       		xre[ b ] = pSample[ a ];
        	xim[ b ] = 0.0f;
        }
        
        float tr, ti, c, s;
        int   k, kn2, x = 0;

        for ( int l = 1; l <= nu; l++ ) {
        	
            k = 0;
            
            while ( k < ss ) {
            	
                for ( int i = 1; i <= n2; i++ ) {
                	
                	// -- Tabled sin/cos
                    c = fftCos[ x ]; 
                    s = fftSin[ x ]; 
                    
                    kn2 = k + n2;
                    
                    tr = xre[ kn2 ] * c + xim[ kn2 ] * s;
                    ti = xim[ kn2 ] * c - xre[ kn2 ] * s;
                    
                    xre[ kn2 ] = xre[ k ] - tr;
                    xim[ kn2 ] = xim[ k ] - ti;
                    xre[ k ] += tr;
                    xim[ k ] += ti;
                    
                    k++; x++;
                    
                }
                
                k += n2;
                
            }
            
            nu1--;
            n2 >>= 1; 
            
        }
        
        int r;
        
        // -- Reorder output.
        for( k = 0; k < ss; k++ ) {
        	
        	// -- Use tabled BR values.
            r = fftBr[ k ]; 
            
            if ( r > k ) {
            	
                tr = xre[ k ];
                ti = xim[ k ];
                
                xre[ k ] = xre[ r ];
                xim[ k ] = xim[ r ];
                xre[ r ] = tr;
                xim[ r ] = ti;
                
            }
            
        }
        
        // -- Calculate magnitude.
        mag[ 0 ] = (float)( Math.sqrt( xre[ 0 ] * xre[ 0 ] + xim[ 0 ] * xim[ 0 ] ) ) / ss;
        
        for ( int i = 1; i < ss2; i++ ) {
            mag[ i ]= 2 * (float)( Math.sqrt( xre[ i ] * xre[ i ] + xim[ i ] * xim[ i ] ) ) / ss;
        }
        
        return mag;
        
    }
    
    private void prepareFFTTables() {
    	
        int n2 = ss2;
        int nu1 = nu - 1;
        
        // -- Allocate FFT SIN/COS tables.
    	fftSin = new float[ nu * n2 ];
    	fftCos = new float[ nu * n2 ];
    	
        float tr, ti, p, arg;
        int   k = 0, x = 0;

        // -- Prepare SIN/COS tables.
        for ( int l = 1; l <= nu; l++ ) {
        	
            while ( k < ss ) {
            	
                for ( int i = 1; i <= n2; i++ ) {
                	
                    p = bitrev( k >> nu1, nu );
                    
                    arg = 2 * (float)Math.PI * p / ss;

                    fftSin[ x ] = (float)Math.sin( arg );
                    fftCos[ x ] = (float)Math.cos( arg );
                    
                    k++;
                    x++;
                    
                }
                
                k += n2;
                
            }
            
            k = 0;
            
            nu1--;
            n2 >>= 1; 
            
        }
        
        // -- Prepare bitrev table.
        fftBr = new int[ ss ];
        
        for( k = 0; k < ss; k++ ) {
            fftBr[ k ] = bitrev( k, nu );
        }
        
    }
	
}