import java.awt.*; public class AliasPlot extends Canvas { Color plotColor = Color.yellow; Color axisColor = Color.red; Color gridColor = Color.green; Color sampleColor = Color.green; Color aliasColor = Color.cyan; Color bgColor = Color.black; int vertSpace = 10; int horzSpace = 10; int vertIntervals = 8; int horzIntervals = 10; int nPoints, nSamples; float xmax = 0.0f; float ymax = 0.0f; private float[] signal, samples, alias; boolean showSignal = true; boolean showSamples = false; boolean showAlias = false; boolean showGrid = false; public AliasPlot() { } public void setPlotColor(Color c) { if (c != null) plotColor = c; } public Color getPlotColor() { return plotColor; } public void setAxisColor(Color c) { if (c != null) axisColor = c; } public Color getAxisColor() { return axisColor; } public void setGridColor(Color c) { if (c != null) gridColor = c; } public Color getGridColor() { return gridColor; } public void setSampleColor(Color c) { if (c != null) sampleColor = c; } public Color getSampleColor() { return sampleColor; } public void setAliasColor(Color c) { if (c != null) aliasColor = c; } public Color getAliasColor() { return aliasColor; } public void setBgColor(Color c) { if (c != null) bgColor = c; } public Color getBgColor() { return bgColor; } public void setVertSpace(int v) { vertSpace = v; } public int getVertSpace() { return vertSpace; } public void setHorzSpace(int h) { horzSpace = h; } public int getHorzSpace() { return horzSpace; } public int getVertIntervals() { return vertIntervals; } public void setVertIntervals(int i) { vertIntervals = i; } public int getHorzIntervals() { return horzIntervals; } public void setHorzIntervals(int i) { horzIntervals = i; } public void setYmax(float m) { ymax = m; } public float getYmax() { return ymax; } public void setSignalData(float[] s) { nPoints = s.length; signal = new float[nPoints]; signal = s; } public void setSamples(float[] s) { nSamples = s.length; samples = new float[nSamples]; samples = s; } public void setAliasData(float[] s) { alias = new float[nPoints]; alias = s; } public void setSignalVisibility(boolean b) { showSignal = b; } public void setSamplesVisibility(boolean b) { showSamples = b; } public void setAliasVisibility(boolean b) { showAlias = b; } public void setGridVisibility(boolean b) { showGrid = b; } public void paint(Graphics g) { int top = vertSpace; int bottom = size().height - vertSpace; int left = horzSpace; int right = size().width - horzSpace; int width = right - left; int fullHeight = bottom - top; int centre = (top + bottom) / 2; int xAxisPos = centre; int yHeight = fullHeight / 2; this.setBackground(bgColor); float xtScale = width/(float)nPoints; float xsScale = width/(float)nSamples; float yScale = yHeight/ymax; int y; int[] xt = new int[nPoints]; // x & y coordinates for int[] yt = new int[nPoints]; // continuous trace plots for (int i = 0; i < nPoints; i++) xt[i] = left + Math.round(i*xtScale); int[] xs = new int[nSamples]; // x coordinates of sample points for (int i = 0; i < nSamples; i++) xs[i] = left + Math.round(i*xsScale); g.setColor(axisColor); g.drawLine(left, top, left, bottom); // vertical axis g.drawLine(left, xAxisPos, right, xAxisPos); // horizontal axis if (showGrid) { g.setColor(gridColor); // draw vertical grid lines to show sample instants int pieces = 40; float dy = fullHeight/(float)pieces; for (int i = 1; i < nSamples; i++) { for (int j = 0; j < pieces; j+=2) { y = top + Math.round(j*dy); g.drawLine(xs[i], y, xs[i], y + Math.round(dy)); } } } if (showSignal) { // plot input sinusoidal waveform g.setColor(plotColor); for (int i = 0; i < nPoints; i++) yt[i] = xAxisPos - Math.round(signal[i]*yScale); for (int i = 0; i < nPoints - 1; i++) g.drawLine(xt[i], yt[i], xt[i+1], yt[i+1]); } if (showSamples) { // plot sample points g.setColor(sampleColor); for (int i = 0; i < nSamples; i++) { y = xAxisPos - Math.round(samples[i]*yScale); g.fillRect(xs[i] - 2, y - 2, 5, 5); } } if (showAlias) { // plot alias frequency waveform g.setColor(aliasColor); for (int i = 0; i < nPoints; i++) yt[i] = xAxisPos - Math.round(alias[i]*yScale); for (int i = 0; i < nPoints - 1; i++) g.drawLine(xt[i], yt[i], xt[i+1], yt[i+1]); } } }