Skip to content

Commit c2e85f0

Browse files
committed
Modified the FFT program to be more generic.
1 parent ce23606 commit c2e85f0

1 file changed

Lines changed: 11 additions & 29 deletions

File tree

source/com/tino1b2be/cmdprograms/TryFFTSpectrum.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,26 @@
55
import org.apache.commons.math3.transform.FastFourierTransformer;
66
import org.apache.commons.math3.transform.TransformType;
77

8-
import com.tino1b2be.dtmfdecoder.DecoderUtil;
9-
import com.tino1b2be.dtmfdecoder.Signals;
10-
118
public class TryFFTSpectrum {
129

1310
public static void main(String[] args) {
14-
double startT = System.currentTimeMillis();
1511
final double Fs = 8000;
1612
final int N = 256;
1713

18-
// *** Generate a signal
14+
// *** Generate a signal with frequency f
1915
double[] signal = new double[N];
2016
System.out.println("Frame length: " + N + " samples => " + (N / Fs) + "ms");
2117

22-
final double f1 = 697.0, f2 = 1209; // Hz
18+
final double f = 1234.0; // Hz
2319
for (int ii = 0; ii < N; ii++) {
24-
signal[ii] = Math.sin(2.0 * Math.PI * f1 * (double) ii / Fs) + Math.sin(2.0 * Math.PI * f2 * (double) ii / Fs) ;
20+
signal[ii] = Math.sin(2.0 * Math.PI * f * (double) ii / Fs);
2521
}
2622

2723
// Should apply hamming window to the frame.
2824

2925
// *** Get power spectrum
3026
final FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
3127

32-
3328
// Note: signal.length should have been a power of 2
3429
final Complex[] spectrum = fft.transform(signal, TransformType.FORWARD);
3530
final double[] powerSpectrum = new double[N / 2 + 1];
@@ -40,27 +35,18 @@ public static void main(String[] args) {
4035

4136
// Expect a sharp peak at around frequency f - index f/Fs *
4237
// signal.length
43-
final double center1 = (f1 / Fs) * N;
44-
final double center2 = (f2 / Fs) * N;
45-
46-
int start, end;
47-
// start = (int) (center - 10.0);
48-
// end = (int) (center + 10.0);
49-
start = 0;
50-
end = powerSpectrum.length;
51-
52-
System.out.println("Center frequency 1 in the FFT: " + center1);
53-
System.out.println("Center frequency 2 in the FFT: " + center2);
38+
final double center = (f / Fs) * N;
39+
final int start = (int) (center - 10.0);
40+
final int end = (int) (center + 10.0);
41+
42+
System.out.println("Center frequency in the FFT: " + center);
5443
for (int ii = start; ii < end; ii++) {
5544
System.out.format("% 3d (% 4.0fHz) power:%01.01f\n", ii, ii * Fs / N, powerSpectrum[ii]);
5645
}
5746

5847
// *** Detect
5948
double totalPower = sum(powerSpectrum);
60-
double totalPower2 = Signals.power(signal);
61-
double totalPower3 = DecoderUtil.signalPower(signal);
62-
63-
double signalPower = powerSpectrum[32] + powerSpectrum[31] + powerSpectrum[33]+ powerSpectrum[55] + powerSpectrum[56] + powerSpectrum[57];
49+
double signalPower = powerSpectrum[39] + powerSpectrum[40];
6450
double ratio = signalPower / totalPower;
6551

6652
// around 80%. Reason being leakage in the FFT. The actual spectrum is a
@@ -69,16 +55,12 @@ public static void main(String[] args) {
6955
// lobe. It's ok though, it just affects the width of the band where you
7056
// look for the actual frequency of interest.
7157
System.out.println("Detection ratio: " + ratio);
72-
73-
double stopT = System.currentTimeMillis();
74-
System.out.println("Time taken = " + Double.toString((stopT-start)/1000) + "sec.");
7558
}
7659

7760
private static double sum(double[] powerSpectrum) {
7861
double s = 0.0;
79-
for (int ii = 0; ii < powerSpectrum.length; ii++) {
80-
s += powerSpectrum[ii];
81-
}
62+
for (double i : powerSpectrum)
63+
s += i;
8264
return s;
8365
}
8466
}

0 commit comments

Comments
 (0)