-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagnitude.java
More file actions
132 lines (97 loc) · 2.55 KB
/
Copy pathMagnitude.java
File metadata and controls
132 lines (97 loc) · 2.55 KB
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
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public
class Magnitude extends Canvas {
private double[] magnitude;
private double[] phase;
private double max_magnitude;
private double min_magnitude;
private final static int WIDTH = 280;
private final static int HEIGHT = 100;
public
Magnitude()
{
super();
setBackground(Color.BLACK);
setForeground(Color.LIGHT_GRAY);
this.addMouseListener( new MouseAdapter() {
public void mousePressed( MouseEvent e ) {
int x = e.getX();
int y = e.getY();
double mag = magnitude[x];
System.out.println( "Magnitude: Magnitude of gray " + x + " :" + mag );
}
} );
setSize( WIDTH, HEIGHT );
}
public
Magnitude( double[][] FFT )
{
this();
compute( FFT );
}
public
void compute( double[][] FFT )
{
magnitude = new double[ FFT.length ];
phase = new double[ FFT.length ];
max_magnitude = min_magnitude = 0;
for( int i=1; i < magnitude.length; i++ ) {
double mag = FFTUtil.magnitude( FFT[i][0], FFT[i][1] );
magnitude[i] = mag;
if ( mag > max_magnitude ) {
max_magnitude = mag;
}
else
if ( mag < min_magnitude ) {
min_magnitude = mag;
}
}
System.out.println( "Magnitude: highest magnitude "+ max_magnitude + " lowest magnitude: " + min_magnitude );
}
public
double getValueAt( int index )
{
return magnitude[index];
}
public
Dimension getMinimumSize()
{
return new Dimension( WIDTH, HEIGHT );
}
public
Dimension getPreferredSize() {
return new Dimension( WIDTH, HEIGHT );
}
public
void paint( Graphics g )
{
Dimension size = getSize();
int x_offset = 10;
int y_top_value = 50;
int x_step = 2;
int MAX_ELEM = (size.width - x_offset) / x_step;
int yCenter = HEIGHT / 2;
double scale = ( (double)y_top_value / max_magnitude);
// g.setColor( Color.BLACK );
// g.drawRect( 0 + x_offset, 0, size.width - x_offset, HEIGHT );
g.drawLine( 0, yCenter, size.width, yCenter);
for( int i = 0; i < size.width; i += 256 )
{
int x = i + x_offset;
g.drawLine( x, 5 , x, yCenter + 5 );
}
g.setColor( Color.YELLOW );
int max_elems = magnitude.length;
if ( max_elems > MAX_ELEM ) max_elems = MAX_ELEM;
// System.out.println( "Magnitude: yScale " + scale );
for(int i=0; i < max_elems; i++ ) {
int y = (int) (magnitude[i*x_step] * scale);
int x = x_offset + x_step * i;
g.drawLine( x, yCenter, x, yCenter - y);
// System.out.println( "Magnitude: x: " + x + " y: " +y );
}
// System.out.println( "Magnitude: max_elems " + max_elems );
}
}