1 | package com.synthbot.jasiohost;
|
2 |
|
3 | public class myDFT {
|
4 | private float sampleRate;
|
5 |
|
6 | private static float sinTime[];
|
7 | private static float cosTime[];
|
8 | private int N;
|
9 | private float freq;
|
10 |
|
11 | public myDFT(float sampleRa,float freq, int N)
|
12 | {
|
13 | this.sampleRate = 44100;
|
14 | this.N = 512;
|
15 | this.freq = 440;
|
16 | sinTime = new float[N];
|
17 | cosTime = new float[N];
|
18 | sinTime = createSinTimeSeries(this.N, this.freq);
|
19 | cosTime = createCosTimeSeries(this.N, this.freq);
|
20 | }
|
21 | public float findFreq (float[] x, int minfreq, int maxfreq, int stepfreq)
|
22 | {
|
23 | float real = 0;
|
24 | float imag = 0;
|
25 | float toreturn=0;
|
26 | float powerAtFreq = 0;
|
27 | float MaxpowerAtFreq = 0;
|
28 | float[] samples = new float[this.N];
|
29 | for (int a = minfreq; a < maxfreq; a += stepfreq)
|
30 | {
|
31 | sinTime = createSinTimeSeries(this.N, a);
|
32 | cosTime = createCosTimeSeries(this.N, a);
|
33 | for(int bb = 0; bb < this.N; bb++)
|
34 | {
|
35 | real += x[bb] * sinTime[bb];
|
36 | imag += x[bb] * cosTime[bb];
|
37 |
|
38 | }
|
39 | powerAtFreq = (real * real) + (imag * imag);
|
40 |
|
41 | if (powerAtFreq > MaxpowerAtFreq)
|
42 | {
|
43 | MaxpowerAtFreq = powerAtFreq;
|
44 | toreturn = a;
|
45 | }
|
46 | }
|
47 | return toreturn;
|
48 |
|
49 | }
|
50 | public float[] inverseDFt(float[] real, float[] imag)
|
51 | {
|
52 | float[] samples = new float[this.N];
|
53 |
|
54 | for (int a = 0; a < this.N; a++)
|
55 | {
|
56 |
|
57 | }
|
58 | return samples;
|
59 | }
|
60 | public float calculateMyDFT(float[] x)
|
61 | {
|
62 | float real = 0;
|
63 | float imag = 0;
|
64 |
|
65 | float powerAtFreq = 0;
|
66 |
|
67 | for(int a = 0; a < this.N; a++)
|
68 | {
|
69 | real += x[a] * sinTime[a];
|
70 | imag += x[a] * cosTime[a];
|
71 | }
|
72 | powerAtFreq = (real * real) + (imag * imag);
|
73 | return powerAtFreq;
|
74 | //System.out.println("Power at freq " + freq + " is " + powerAtFreq);
|
75 | }
|
76 |
|
77 | public float[] createSinTimeSeries(int N, float freq)
|
78 | {
|
79 | float output[] = new float[N];
|
80 | for (int i = 0; i < N; i++) {
|
81 | output[i] = (float) (Math.sin(2 * Math.PI * i * freq / this.sampleRate));
|
82 | }
|
83 | return output;
|
84 |
|
85 | }
|
86 | public float[] createCosTimeSeries(int N, float freq)
|
87 | {
|
88 | float output[] = new float[N];
|
89 | for (int i = 0; i < N; i++) {
|
90 | output[i] = (float) (Math.cos(2 * Math.PI * i * freq / this.sampleRate));
|
91 | }
|
92 | return output;
|
93 | }
|
94 | }
|