FFT变化与反变换滤波分析

using System;
using MathNet.Numerics;
using MathNet.Numerics.IntegralTransforms;
using MathNet.Numerics.LinearAlgebra;

public class Filter
{
    public static double[] Apply(double[] input, int dimension, double lowerCutoff, double higherCutoff, double sampleRate)
    {
        // 计算 FFT
        Complex32[] fftInput = Array.ConvertAll(input, x => new Complex32(Convert.ToSingle(x), 0));
        Fourier.Forward(fftInput, FourierOptions.Matlab);

        // 创建掩码
        int n = fftInput.Length;
        double[] frequencies = new double[n];
        for (int i = 0; i < n; i++)
        {
            frequencies[i] = i * (sampleRate / n);
        }
        bool[] mask = new bool[n];
        for (int i = 0; i < n; i++)
        {
            mask[i] = frequencies[i] > lowerCutoff && frequencies[i] < higherCutoff;
        }

        // 应用掩码
        for (int i = 0; i < n; i++)
        {
            if (!mask[i])
            {
                fftInput[i] = Complex32.Zero;
            }
        }

        // 计算 IFFT
        Fourier.Inverse(fftInput, FourierOptions.Matlab);

        // 转换回 double 数组
        double[] output = Array.ConvertAll(fftInput, x => Convert.ToDouble(x.Real));

        return output;
    }
}

以下为对应Matlab代码,转载自他人博客

% FILTERED = ideal_bandpassing(INPUT,DIM,WL,WH,SAMPLINGRATE)
% 
% Apply ideal band pass filter on INPUT along dimension DIM.
% 
% WL: lower cutoff frequency of ideal band pass filter
% WH: higher cutoff frequency of ideal band pass filter
% SAMPLINGRATE: sampling rate of INPUT
% 
% Copyright (c) 2011-2012 Massachusetts Institute of Technology, 
% Quanta Research Cambridge, Inc.
%
% Authors: Hao-yu Wu, Michael Rubinstein, Eugene Shih, 
% License: Please refer to the LICENCE file
% Date: June 2012
%
function filtered = ideal_bandpassing(input, dim, wl, wh, samplingRate)
 
    if (dim > size(size(input),2))
        error('Exceed maximum dimension');
    end
 
    input_shifted = shiftdim(input,dim-1);
    Dimensions = size(input_shifted);
    
    n = Dimensions(1);
    dn = size(Dimensions,2);
    
    
    Freq = 1:n;
    Freq = (Freq-1)/n*samplingRate;
    mask = Freq > wl & Freq < wh;
    
    Dimensions(1) = 1;
    mask = mask(:);
    mask = repmat(mask, Dimensions);
 
    
    F = fft(input_shifted,[],1);
    
    F(~mask) = 0;
    
    filtered = real(ifft(F,[],1));
    
    filtered = shiftdim(filtered,dn-(dim-1));
    
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

溧阳苏东坡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值