FFT(Fast Fourier Transformation)是离散傅氏变换(DFT)的快速算法。即为快速傅氏变换。它是根据离散傅氏变换的奇、偶、虚、实等特性,对离散傅立叶变换的算法进行改进获得的。
FFT是一种DFT的高效算法,称为快速傅立叶变换(fast Fourier transform),它根据离散傅氏变换的奇、偶、虚、实等特性,对离散傅立叶变换的算法进行改进获得的。FFT算法可分为按时间抽取算法和按频率抽取算法,先简要介绍FFT的基本原理。从DFT运算开始,说明FFT的基本原理。
DFT的运算为:
式中
由这种方法计算DFT对于***X(k)**的每个K值,需要进行4N次实数相乘和(4N-2)次相加,对于N个k值,共需4NN次实数相乘和(4N-2)*N次实数相加。改进DFT算法,减小它的运算量,利用DFT中的周期性和对称性,使整个DFT的计算变成一系列迭代运算,可大幅度提高运算过程和运算量,这就是FFT的基本思想。
FFT对傅氏变换的理论并没有新的发现,但是对于在计算机系统或者说数字系统中应用离散傅立叶变换,可以说是进了一大步。
设x(n)为N项的复数序列,由DFT变换,任一X(m)的计算都需要N次复数乘法和N-1次复数加法,而一次复数乘法等于四次实数乘法和两次实数加法,一次复数加法等于两次实数加法,即使把一次复数乘法和一次复数加法定义成一次“运算”(四次实数乘法和四次实数加法),那么求出N项复数序列的X(m),即N点DFT变换大约就需要N2次运算。当N=1024点甚至更多的时候,需要N2=1048576次运算,在FFT中,利用WN的周期性和对称性,把一个N项序列(设N=2k,k为正整数),分为两个N/2项的子序列,每个N/2点DFT变换需要(N/2)2次运算,再用N次运算把两个N/2点的DFT变换组合成一个N点的DFT变换。这样变换以后,总的运算次数就变成N+2*(N/2)2=N+(N^2)/2。继续上面的例子,N=1024时,总的运算次数就变成了525312次,节省了大约50%的运算量。而如果我们将这种“一分为二”的思想不断进行下去,直到分成两两一组的DFT运算单元,那么N点的DFT变换就只需要N/2log2N次的运算,N在1024点时,运算量仅有5120次,是先前的直接算法的近1/200,点数越多,运算量的节约就越大,这就是FFT的优越性。
(以上定义来源于百度百科)
带通滤波器(band-pass filter)是一个允许特定频段的波通过同时屏蔽其他频段的设备.在本文我们需要的是特定频段虑掉其他频段,如本文案例一样保留1-20频率的数据。
下面附上JAVA实现FFT变换的带通滤波算法代码:
import java.util.Objects;
import edu.princeton.cs.algs4.StdOut;
public class Complex {
private final double re; // the real part
private final double im; // the imaginary part
//相关系数设置
private static int Fs = 250;
private static int N = 1024;//数据大小
private static int[] n = new int[N];//flag
private static double[] f = new double[N];//频率数组
private static int f1 = 1;//滤波flag设置
private static int f2 = 20;//滤波flag设置
private static double dt = 0.001;
private static int flag = 0;
private static double flag2 = 0.0;
private static Complex flag1 = new Complex(0, 0);
private static double[] Z = new double[N];
private static Complex[] yy = new Complex[N];
// 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);
}
// compute the FFT of x[], assuming its length is a power of 2
public static Complex[] fft(Complex[] x) {
int n = x.length;
// base case
if (n == 1) return new Complex[] { x[0] };
// radix 2 Cooley-Tukey FFT
if (n % 2 != 0) {
throw new IllegalArgumentException("n is not a power of 2");
}
// fft of even terms
Complex[] even = new Complex[n/2];
for (int k = 0; k < n/2; k++) {
even[k] = x[2*k];
}
Complex[] q = fft(even);
// fft of odd terms
Complex[] odd = even; // reuse the array
for (int k = 0; k < n/2; k++) {
odd[k] = x[2*k + 1];
}
Complex[] r = fft(odd);
// combine
Complex[] y = new Complex[n];
for (int k = 0; k < n/2; k++) {
double kth = -2 * k * Math.PI / n;
Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
y[k] = q[k].plus(wk.times(r[k]));
y[k + n/2] = q[k].minus(wk.times(r[k]));
}
return y;
}
public static void show(Complex[] x, String title) {
StdOut.println(title);
StdOut.println("-------------------");
for (int i = 0; i < x.length; i++) {
StdOut.println(x[i]);
}
StdOut.println();
}
public static void show(double[] x, String title) {
StdOut.println(title);
StdOut.println("-------------------");
for (int i = 0; i < x.length; i++) {
StdOut.println(x[i]);
}
StdOut.println();
}
public static double bandpass(Complex[] y) {
/*for(int i = 0; i < N; i++){
n[i] = i;
}*/
//设置频率数组
for(flag = 0; flag < N; flag++) {
f[flag] = flag*Fs/N;
}
//滤波
for(flag = 0; flag < N; flag++) {
if(flag/(N*dt) < f1||flag/(N*dt) > f2) {
yy[flag] = flag1;
}else {
yy[flag] = y[flag];
}
}
for(flag = 0; flag < N; flag++) {
Z[flag] = yy[flag].abs()*2/N;
}
for(flag = 0; flag < N-1; flag++) {
if(Z[flag] < Z[flag + 1]) {
Z[flag] = Z[flag + 1];
flag2 = f[flag];
}
}
return flag2;
}
// 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);
int n = 1024;
Complex[] x = new Complex[n];
double[] A = new double[n];
double res = 1;
// original data
for (int i = 0; i < n; i++) {
//x[i] = new Complex(i, 0);
x[i] = new Complex(-2*Math.random() + 1, 0);
}
show(x, "x");
Complex[] y = new Complex[n];
y = fft(x);
show(y, "y = fft(x)");
for(flag = 0; flag < n; flag++) {
A[flag] = y[flag].abs();
}
show(A, "a = abs(y)");
res = bandpass(y);
System.out.println(res);
}
}
这里显示用到了一个StdOut库,百度云盘下载地址:链接:https://pan.baidu.com/s/1A3g4_fJ4KcJcGrz5u_QfSA
提取码:r50b (推荐大家尽量在云盘下载)
本文代码下载地址:https://download.csdn.net/download/systemlsy/11125191