fourier transfrom 傅立叶变换代码实现(matlab语言)

2024-06-06 10:18

本文主要是介绍fourier transfrom 傅立叶变换代码实现(matlab语言),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

DFT的事情拖了很久了,上个学期就一直纠缠着,始终没有理解,去逸夫楼上课每次下课空教室的时候就喜欢一个人“过瘾”,在黑板上写DFT

的表达式,总想着程序实现。大概那样过了一个月,还是没有理解(智商是硬伤),这几天看了stanford的视频,老师挺逗的,感觉好像懂了,

于是重新写DFT。在matlab下实现了 。。。。




个人觉得<The Scientist and Engineer's Guide to Digital Signal Processing>这本书还是很好的。很值得一看,如果对DSP有兴趣的话。

好吧,开源,上代码:

%% ***************************************************************************************
% code writer ?EOF
% code date : 2014.03.11              2014.03.12 update
% e-mail : jasonleaster@gmail.com
% code purpose : 
%       This code is demo for DFT. I would like to share my code with
%       someone who is interesting in DSP. If there is something wrong with
%       my code, please touche me by e-mail. Thank you!
%
%% ***************************************************************************************TotalSample = 400;
%The number of all the signal that our sensor got
circle = TotalSample/2;
% circle = TotalSample;
%We assume that the preiod of the signal we generated is 'circle';
SignalInS = zeros(circle,1);
%This varible is used for recording the signal in frequency domain
SignalInT = zeros(TotalSample,1);
%This varible is used for recording the signal which were processed by inverse-DFT in time domain
OriginalSignal = zeros(TotalSample,1);
%This varible is used for recording the original signal that we got.%% initialize a square wave
for SampleNumber = -(TotalSample/2):(TotalSample/2)-1if (mod(abs(SampleNumber),circle) < (circle/2))&&(SampleNumber>0)OriginalSignal((TotalSample/2)+1+SampleNumber) = 5;elseif (mod(abs(SampleNumber),circle) >= (circle/2))&&(SampleNumber>0)OriginalSignal((TotalSample/2)+1+SampleNumber) = 0;elseif (mod(abs(SampleNumber),circle) < (circle/2))&&(SampleNumber<0)OriginalSignal((TotalSample/2)+1+SampleNumber) = 0;   elseif (mod(abs(SampleNumber),circle) >= (circle/2))&&(SampleNumber<0)OriginalSignal((TotalSample/2)+1+SampleNumber) = 5;end
end% %% initialize a delta wave
% for SampleNumber = -(TotalSample/2):(TotalSample/2)-1
%     if (SampleNumber == 0)
%         OriginalSignal((TotalSample/2)+1+SampleNumber) = 5;
%     end
% end%We show the original signal in time domain.
figure(1);
plot( -(TotalSample/2):(TotalSample/2)-1,OriginalSignal,'.-');
title('The original signal');%% forward-DFT
for frequency = -(circle/2):(circle/2)-1 % You must knew what is "Shannon sampling theorem" and you will knew that why frequency is from -(circle/2) to (circle/2)-1for SampleNumber = -(TotalSample/2):(TotalSample/2)-1%Nothingelse, just from 1 to TotalSampleSignalInS((circle/2)+1+frequency) = ...SignalInS((circle/2)+1+frequency) + ...OriginalSignal((TotalSample/2)+1+SampleNumber)*exp(-2*pi*(frequency/circle)*(SampleNumber)*i);end
end%We show the real part of processed signal in frequency domain.
figure(2);
plot(-(circle/2):(circle/2)-1,real(SignalInS),'-');
title('The real part of signal after processed');%We show the imagine part of processed signal in frequency domain.
figure(3);
plot(-(circle/2):(circle/2)-1,imag(SignalInS),'-');
title('The imagine part of signal after processed');%% inverse-DFT
for frequency = -(circle/2):(circle/2)-1for SampleNumber = -(TotalSample/2):(TotalSample/2)-1SignalInT((TotalSample/2)+1+SampleNumber) = ...SignalInT((TotalSample/2)+1+SampleNumber) + ...(1/TotalSample)*SignalInS((circle/2)+1+frequency)*exp(2*pi*(frequency/circle)*(SampleNumber)*i);end
end%Just show the signal that we rebuilt by inverse-DFT
figure(4);
plot(-(TotalSample/2):(TotalSample/2)-1,SignalInT,'-');
title('The  rebuilt signal after processed');





我们假设初始信号是方波信号



处理后的实数部分


处理后的虚数部分


逆变换合成的信号



可以看出逆变换的效果是很好的。

这里应该注意

circle = TotalSample/2;

信号周期不能太短,太短了就会出现aliasing引起的逆向变换误差很大。设置值的时候,根据shannon sampling定理来模拟相应的信号周期就可以了


It is hard and wonderful time when you are thinking about fourier transform.


2014.03.12更新:

原来的代码有点问题,对于delta函数的傅立叶变换显然不正确。现在对代码作出更正


The . L

于XTU



update: 2014.10.29

方波的初始化部分可以用以下算法,使得方波关于y轴对称

%% initialize a square wave
for SampleNumber = -(TotalSample/2):(TotalSample/2)-1if (mod(abs(SampleNumber),circle) <= (circle/4)) || (mod(abs(SampleNumber),circle) > (circle*3/4))OriginalSignal((TotalSample/2)+1+SampleNumber) = 5;elseOriginalSignal((TotalSample/2)+1+SampleNumber) = 0;end
end









这篇关于fourier transfrom 傅立叶变换代码实现(matlab语言)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035822

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

C语言进阶(预处理命令详解)

《C语言进阶(预处理命令详解)》文章讲解了宏定义规范、头文件包含方式及条件编译应用,强调带参宏需加括号避免计算错误,头文件应声明函数原型以便主函数调用,条件编译通过宏定义控制代码编译,适用于测试与模块... 目录1.宏定义1.1不带参宏1.2带参宏2.头文件的包含2.1头文件中的内容2.2工程结构3.条件编

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库