使用Java实现DFT

最近在数字信号处理这么课程里面学习了DFT,FFT
要想实现FFT,我想先从DFT入手比较简单
里面会有比较多的相似之处

程序代码

package FFT;

public class DITFFTinputinorder {
    static double Pi = Math.PI;
    public static void main(String[] args) {

        double para;
        float sin,cos;
        double tolerance = 1e-10;  // 容差范围
        ComplexNumber[] input = new ComplexNumber[6];
        ComplexNumber[] output = new ComplexNumber[6];
        ComplexNumber W = new ComplexNumber();
        input[0] = new ComplexNumber(1, 0);
        input[1] = new ComplexNumber(2, 0);
        input[2] = new ComplexNumber(4, 0);
        input[3] = new ComplexNumber(3, 0);
        input[4] = new ComplexNumber(0, 0);
        input[5] = new ComplexNumber(5, 0);

        for (int i = 0; i < 6; i++) {
            output[i] = new ComplexNumber();
        }
        for (int k = 0; k < 6; k++) {
            for (int n = 0; n < 6; n++) {
                para = 0-(n * k * 2 * Pi) / 6;
                sin = (float) Math.sin(para);
                cos = (float) Math.cos(para);
                if (Math.abs(sin) < tolerance) {
                    sin  = 0.0F;
                }
                if(Math.abs(cos) < tolerance){
                    cos = 0.0F;
                }
                W.setW(cos,sin);
                output[k].add(input[n].multiply(W));      //直接计算DFT
            }
            System.out.println(output[k].toString());
        }
    }
}

class ComplexNumber {
    private double real;
    private double imaginary;

    public ComplexNumber() {
        // 默认构造函数
    }
    public ComplexNumber(double real, double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }

    public double getReal() {
        return real;
    }

    public double getImaginary() {
        return imaginary;
    }
    public void setW(double real,double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }
    public ComplexNumber multiply(ComplexNumber other) {
        double resultReal = this.real * other.real - this.imaginary * other.imaginary;
        double resultImaginary = this.real * other.imaginary + this.imaginary * other.real;
        return new ComplexNumber(resultReal, resultImaginary);
    }

    @Override
    public String toString() {
        if(real == 0.0)
            real = (int) real;
        return "(" + real + " + " + imaginary + "i)";
    }

    public void add(ComplexNumber other) {
        this.real += other.real;
        this.imaginary += other.imaginary;
    }
}

问题分析

还是和之前一样,我们来分析一下这个问题
入手点(目标):DFT公式

求和表示

一个简单的循环即可。需要运用X[k],就再加一层循环即可。

乘法计算

很重要的一个问题就是这些计算都是复数计算
复数乘法,复数加法
所以定义复数类来处理计算

计算精度

计算精度不要太高
太高可能会使结果太“精准”。应为这里面的三角函数与我们的计算不同。计算机,你懂的。
现实中的三角函数计算与计算机不同。例如计算机计算sin(Π)的结果是在这里插入图片描述
这是会有误差的,程序里的Pi只是一个约值,不可能是无限不循环小数。
所以还需要加入容差修正


ps:这个错误找了我好久,气死了!!!

写的头痛!可能是对DFT的计算和技巧不够熟悉,理解不够深入。
气死了,两级无法自定义文章标签(╯▔皿▔)╯

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值