重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
matlab实现的代码:
成都创新互联公司主要从事成都网站设计、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务上党,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
x=importdata('aa.txt') %从aa.txt文件中读取数据,64点FFT就取64点数据
n=[1:64]; %64个数据
N=64;
y=fft(x); %进行FFT计算
%输出y
M=abs(y); %取幅值
M(1)=M(1)/2;
plot(n,2*M/N); %绘制幅频图,
title('幅频相应');
xlabel('频率');
ylabel('幅度');
如果要单片机实现的话,cortex及ARM有相应的库函数,但是要注意采样率,采样周期与信号周期的关系,频谱泄露的影响。
首先要先把arm音频解码后,分帧处理(如128个样点),然后用fft函数就可以了。
从你的问题描述看你是问是否有一个复数类型complex,我查了一下没有,都是自己定义的。下面的代码你可以参考:
/******************************************************************************
* Compilation: javac Complex.java
* Execution: java Complex
*
* Data type for complex numbers.
*
* The data type is "immutable" so once you create and initialize
* a Complex object, you cannot change it. The "final" keyword
* when declaring re and im enforces this rule, making it a
* compile-time error to change the .re or .im instance variables after
* they've been initialized.
*
* % java Complex
* a = 5.0 + 6.0i
* b = -3.0 + 4.0i
* Re(a) = 5.0
* Im(a) = 6.0
* b + a = 2.0 + 10.0i
* a - b = 8.0 + 2.0i
* a * b = -39.0 + 2.0i
* b * a = -39.0 + 2.0i
* a / b = 0.36 - 1.52i
* (a / b) * b = 5.0 + 6.0i
* conj(a) = 5.0 - 6.0i
* |a| = 7.810249675906654
* tan(a) = -6.685231390246571E-6 + 1.0000103108981198i
*
******************************************************************************/
import java.util.Objects;
public class Complex {
private final double re; // the real part
private final double im; // the imaginary part
// create a new object with the given real and imaginary parts
public Complex(double real, double imag) {
re = real;
im = imag;
}
// return a string representation of the invoking Complex object
public String toString() {
if (im == 0) return re + "";
if (re == 0) return im + "i";
if (im 0) return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
// return abs/modulus/magnitude
public double abs() {
return Math.hypot(re, im);
}
// return angle/phase/argument, normalized to be between -pi and pi
public double phase() {
return Math.atan2(im, re);
}
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this; // invoking object
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
// return a new Complex object whose value is (this * b)
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
return new Complex(real, imag);
}
// return a new object whose value is (this * alpha)
public Complex scale(double alpha) {
return new Complex(alpha * re, alpha * im);
}
// return a new Complex object whose value is the conjugate of this
public Complex conjugate() {
return new Complex(re, -im);
}
// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
double scale = re*re + im*im;
return new Complex(re / scale, -im / scale);
}
// return the real or imaginary part
public double re() { return re; }
public double im() { return im; }
// return a / b
public Complex divides(Complex b) {
Complex a = this;
return a.times(b.reciprocal());
}
// return a new Complex object whose value is the complex exponential of this
public Complex exp() {
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
}
// return a new Complex object whose value is the complex sine of this
public Complex sin() {
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex cosine of this
public Complex cos() {
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
}
// return a new Complex object whose value is the complex tangent of this
public Complex tan() {
return sin().divides(cos());
}
// a static version of plus
public static Complex plus(Complex a, Complex b) {
double real = a.re + b.re;
double imag = a.im + b.im;
Complex sum = new Complex(real, imag);
return sum;
}
// See Section 3.3.
public boolean equals(Object x) {
if (x == null) return false;
if (this.getClass() != x.getClass()) return false;
Complex that = (Complex) x;
return (this.re == that.re) (this.im == that.im);
}
// See Section 3.3.
public int hashCode() {
return Objects.hash(re, im);
}
// sample client for testing
public static void main(String[] args) {
Complex a = new Complex(5.0, 6.0);
Complex b = new Complex(-3.0, 4.0);
StdOut.println("a = " + a);
StdOut.println("b = " + b);
StdOut.println("Re(a) = " + a.re());
StdOut.println("Im(a) = " + a.im());
StdOut.println("b + a = " + b.plus(a));
StdOut.println("a - b = " + a.minus(b));
StdOut.println("a * b = " + a.times(b));
StdOut.println("b * a = " + b.times(a));
StdOut.println("a / b = " + a.divides(b));
StdOut.println("(a / b) * b = " + a.divides(b).times(b));
StdOut.println("conj(a) = " + a.conjugate());
StdOut.println("|a| = " + a.abs());
StdOut.println("tan(a) = " + a.tan());
}
}
二维FFT相当于对行和列分别进行一维FFT运算。具体的实现办法如下:
先对各行逐一进行一维FFT,然后再对变换后的新矩阵的各列逐一进行一维FFT。相应的伪代码如下所示:
for (int i=0; iM; i++)
FFT_1D(ROW[i],N);
for (int j=0; jN; j++)
FFT_1D(COL[j],M);
其中,ROW[i]表示矩阵的第i行。注意这只是一个简单的记法,并不能完全照抄。还需要通过一些语句来生成各行的数据。同理,COL[i]是对矩阵的第i列的一种简单表示方法。
所以,关键是一维FFT算法的实现。下面讨论一维FFT的算法原理。
【1D-FFT的算法实现】
设序列h(n)长度为N,将其按下标的奇偶性分成两组,即he和ho序列,它们的长度都是N/2。这样,可以将h(n)的FFT计算公式改写如下 :
(A)
由于
所以,(A)式可以改写成下面的形式:
按照FFT的定义,上面的式子实际上是:
其中,k的取值范围是 0~N-1。
我们注意到He(k)和Ho(k)是N/2点的DFT,其周期是N/2。因此,H(k)DFT的前N/2点和后N/2点都可以用He(k)和Ho(k)来表示