用java实现动画演示香农采样定理
时间: 2023-12-19 20:05:38 浏览: 114
二维抽样定理(惠特克-香农抽样定理,Whittaker_Shannon sampling theorem)利用抽样函数重构原函数的过程和还原条件。
很抱歉,作为AI语言模型,我的能力是生成文本,无法运行实际的Java代码或制作动画。但是,我可以为您提供一个可以参考的Java代码框架,您可以根据需要进行修改和完善。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShannonSamplingDemo extends JFrame implements ActionListener {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int SAMPLE_RATE = 2000;
private static final int SIGNAL_FREQUENCY = 100;
private static final int NOISE_FREQUENCY = 400;
private static final int NOISE_AMPLITUDE = 30;
private Timer timer;
private int time;
private int[] signal;
private int[] noise;
private int[] sample;
public ShannonSamplingDemo() {
super("Shannon Sampling Demo");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
timer = new Timer(20, this);
// generate signal and noise
signal = new int[WIDTH];
noise = new int[WIDTH];
for (int i = 0; i < WIDTH; i++) {
signal[i] = (int) (Math.sin(2 * Math.PI * SIGNAL_FREQUENCY * i / SAMPLE_RATE) * 100);
noise[i] = (int) (Math.sin(2 * Math.PI * NOISE_FREQUENCY * i / SAMPLE_RATE) * NOISE_AMPLITUDE);
}
// sample signal
sample = new int[WIDTH / 2];
int index = 0;
for (int i = 0; i < WIDTH; i += 2) {
sample[index++] = signal[i];
}
setVisible(true);
timer.start();
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
// draw signal
g.setColor(Color.RED);
for (int i = 0; i < WIDTH - 1; i++) {
g.drawLine(i, HEIGHT / 2 - signal[i], i + 1, HEIGHT / 2 - signal[i + 1]);
}
// draw noise
g.setColor(Color.BLUE);
for (int i = 0; i < WIDTH - 1; i++) {
g.drawLine(i, HEIGHT / 2 - noise[i], i + 1, HEIGHT / 2 - noise[i + 1]);
}
// draw sample
g.setColor(Color.BLACK);
for (int i = 0; i < WIDTH / 2 - 1; i++) {
g.drawLine(i * 2, HEIGHT / 2 - sample[i], (i + 1) * 2, HEIGHT / 2 - sample[i + 1]);
}
// draw time
g.setColor(Color.BLACK);
g.drawString("Time: " + time + "ms", 10, 20);
}
public void actionPerformed(ActionEvent e) {
time += 20;
if (time >= 2000) {
timer.stop();
}
repaint();
}
public static void main(String[] args) {
new ShannonSamplingDemo();
}
}
```
这个代码框架实现了一个简单的香农采样定理动画演示,其中生成了一个信号和一个噪声信号,并对信号进行采样。在每一帧中,程序将绘制信号、噪声信号和采样信号的图像,并显示当前时间。您可以根据需要进行修改和完善,例如添加更详细的注释,增加交互式控件等等。
阅读全文