使用单片机和FFT算法显示波形(高分!!!急救!!)

使用单片机和FFT算法显示波形(高分!!!急救!!),第1张

fft算法是频谱分析,输入电流或电压信号首先要使用模-数转换电路,根据精度和速度的不同要求,采用的电路也差别很大。fft的输入不外乎就是一串采样数据,以及这些数据的采样时间间隔是多少,这个你需要自己去分析或者代码中有注解就更好。fft最终可能会输出一个数组作为它的分析结果,你可以通过串口输入到电脑中,电脑通过串口接收到单片机发过来的数据以后,就可以通过一些数据分析工具把曲线显示出来。我见过用fft算法配合高速采样电路来分析钢琴音准和音色(即频谱)的实际产品,价格很贵的哦。

matlab中一般把数据存为mat文件,例如存fft的数据的话可以这样

a=fft(b);

save fftmat 'a'

fftmat中的fft是文件名,可以符合命名格式的前提任意取,而‘mat’是文件格式,后面的'a'就是要存的变量,这样的话就会在当前路径下看到fftmat这个文件了

如果要调用fftmat里面的数据的话就使用load命令,如:

load fftmat

这样就相当于把上面存好的数据调出来。至于你说的“输出数据txt“我没试过,不知道可不可以

FFTW3 is a library designed to compute discrete Fourier transforms As described in the  the official FFTW site , there are various versions available, with different features and different levels of maturity In this tutorial I deal with the installation of version 3, including the experimental MPI version However the installation instructions seems to be valid (although not tested) also for the more popular version 2

Contents

[ hide ]

1 General Remarks

2 Build and Install

21 Serial version only

22 MPI version

3 Compilation with Shared Libraries

4 Basic Usage

41 Serial

42 Parallel MPI

421 Important notes about MPI-FFTW3 memory distribution (do not skip)

General Remarks

As usual we would like to install the libraries in the user space, so we will create a couple of directories for that purpose:

To install FFTW3, download the package from the  FFTW3 download page  and decompress it:

Ubuntu only:  If you want to install FFTW3 (serial version) in your local Ubuntu you can skip this installation section altogether and just run:

sudo apt-get install libfftw3-dev libfftw3-doc

However the MPI version (eg for testing) will not be available If you want to have the MPI version follow the instructions in the other sections

Build and Install

The configure/make/install procedure works well for installation in wcrstanfordedu We have the option of building and using static or shared libraries If you are going to use shared libraries read last section

Serial version only

Then configure, make and install:

(1 minute) The following files will be installed in:

The typical compilation options will be

The official tutorial on the usage of FFTW3 (which is different from FFTW 2) is located  here

MPI version

To install the experimental MPI version of FFTW3, make sure you downloaded fftw-33alpha1 (and not fftw-32 for example) Also make sure that there is an MPI compiler available:

If it is not available, you can choose one with the command 'mpi-selector-menu' in wcr I tested this with the 'openmpi_gcc-122' compiler If that is not possible then set the variable MPICC, for example: export MPICC=$HOME/usr/bin/mpicc

Do the same procedure of downloading the file and decompressing it, but add the --enable-mpi flag:

now the library will be installed in your home directory, besides the files mentioned above, you will find also:

The typical command line for compilation will be

Make sure to link first to fftw3_mpi and later to fftw3 For some MPI platforms (notably openmpi-gcc) setting LD_RUN_PATH does not do the job of storing the library path inside the executables (see note in previous section), it may be necessary to use the following command:

In any case we should always check that the executable is properly linked by doing

and checking that all shared libraries are "found"

The official tutorial for the MPI version of FFTW3 can be found  here

Compilation with Shared Libraries

In the command line compilation examples above I set the variables LD_RUN_PATH Using LD_RUN_PATH saves us from having to set path variables before running the program, such as LD_LIBRARY_PATH (which is a  bad practice ) When LD_RUN_PATH is set before compilation, the created executable will store the search path of the shared library internally (but will not enforce it) I learned this trick from  >

因为N个样点的信号经过fft以后变成N个样点的频谱,这个频谱是关于第N/2+1样点左右对称的,所以真正有用的频谱数据只有前面一半,后面一半是镜像。mxk11是对前N/2个样点取幅度谱,其实应该是取1:N1/2+1,你这里少取了一个点。具体为什么会镜像请看数字信号处理DFT章节。

1 、一般频域的采样点要大于时域的采样点,最好是2的幂数,便于计算。可以看看数字信号处理这类的书 2、 假设采样频率为Fs,信号频率F,采样点数为N。那么FFT之后结果就是一个为N点的复数。每一个点就对应着一个频率点。这个点的模值,就是该频率值下的幅度特性。具体跟原始信号的幅度有什么关系呢?假设原始信号的峰值为A,那么FFT的结果的每个点(除了第一个点直流分量之外)的模值就是A的N/2倍 所以这里应该是 3 linspace(x0,x1,n) 其中n代表的是点的数目,即分成n-1等分。其实Fs/2linspace(0,1,NFFT/2+1);就是在0到1之间分成NFFT/2份,也就是FS/NFFT,也就是设置间隔点的频率。最后2abs(Y(1:NFFT/2+1)) 因为前面Y = fft(x,NFFT)/ NFFT 是原来信号的二分之一 所以要乘以2

假设FFT之后某点n用复数a+bi表示,那么这个复数的模就是

An=根号aa+bb,相位就是Pn=atan2(b,a)。根据以上的结果,

就可以计算出n点(n≠1,且n<=N/2)对应的信号的表达式为:

An/(N/2)cos(2piFnt+Pn),即2An/Ncos(2piFnt+Pn)。

对于n=1点的信号,是直流分量,幅度即为A1/N。

由于FFT结果的对称性,通常我们只使用前半部分的结果,

即小于采样频率一半的结果。

在图象处理的广泛应用领域中,傅立叶变换起着非常重要的作用,具体表现在包括图象分析、图象增强及图象压缩等方面。

fftshift的作用正是让正半轴部分和负半轴部分的图像分别关于各自的中心对称。因为直接用fft得出的数据与频率不是对应的,fftshift可以纠正过来。

假设f(x,y)是一个离散空间中的二维函数,则该函数的二维傅立叶变换的定义如下:

p=0,1…M-1 q=0,1…N-1 (1)

或 p=0,1…M-1 q=0,1…N-1 (2)

离散傅立叶反变换的定义如下:

m=0,1…M-1 n=0,1…N-1(3)

F(p,q)称为f(m,n)的离散傅立叶变换系数。这个式子表明,函数f(m,n)可以用无数个不同频率的复指数信号和表示,而在频率(w1,w2)处的复指数信号的幅度和相位是F(w1,w2)。

2、MATLAB提供的快速傅立叶变换函数

(1)fft2

fft2函数用于计算二维快速傅立叶变换,其语法格式为:

B = fft2(I)

B = fft2(I)返回图象I的二维fft变换矩阵,输入图象I和输出图象B大小相同。

例如,计算图象的二维傅立叶变换,并显示其幅值的结果,如图所示,其命令格式如下

load imdemos saturn2

imshow(saturn2)

B = fftshift(fft2(saturn2));

imshow(log(abs(B)),[],'notruesize')

(2)fftshift

MATLAB提供的fftshift函数用于将变换后的图象频谱中心从矩阵的原点移到矩阵的中心,其语法格式为:

B = fftshift(I)

对于矩阵I,B = fftshift(I)将I的一、三象限和二、四象限进行互换。

(2)ifft2

ifft2函数用于计算图象的二维傅立叶反变换,其语法格式为:

B = ifft2(I)

B = ifft2(A)返回图象I的二维傅立叶反变换矩阵,输入图象I和输出图象B大小相同。其语法格式含义与fft2函数的语法格式相同,可以参考fft2函数的说明。

如果信号是二维的,用上面的函数即可!直接调用。

如果信号是一维的,给你下面的例子,你应该能明白!

clear

fs=100;N=128; %采样频率和数据点数

n=0:N-1;t=n/fs; %时间序列

x=05sin(2pi15t)+2sin(2pi40t); %信号

y=fft(x,N); %对信号进行快速Fourier变换,逆变换函数为ifft

mag=abs(y); %求得Fourier变换后的振幅

f=nfs/N; %频率序列

subplot(2,2,1),plot(f,mag); %绘出随频率变化的振幅

xlabel('频率/Hz');

ylabel('振幅');title('N=128');grid on;

subplot(2,2,2),plot(f(1:N/2),mag(1:N/2)); %绘出Nyquist频率之前随频率变化的振幅

xlabel('频率/Hz');

ylabel('振幅');title('N=128');grid on;

%对信号采样数据为1024点的处理

fs=100;N=1024;n=0:N-1;t=n/fs;

x=05sin(2pi15t)+2sin(2pi40t); %信号

y=fft(x,N); %对信号进行快速Fourier变换

mag=abs(y); %求取Fourier变换的振幅

f=nfs/N;

subplot(2,2,3),plot(f,mag); %绘出随频率变化的振幅

xlabel('频率/Hz');

ylabel('振幅');title('N=1024');grid on;

subplot(2,2,4)

plot(f(1:N/2),mag(1:N/2)); %绘出Nyquist频率之前随频率变化的振幅

xlabel('频率/Hz');

ylabel('振幅');title('N=1024');grid on;

看看下面的程序,应该能帮上你的忙,已经通过调试:

Fs=256;

%采样频率(Hz)

N=256;

%采样点数

t=[0:1/Fs:N/Fs];

%采样时刻

S=2+3cos(2pi10t+pi30/180)+cos(2pi20t+pi90/180);

%我的调试信号,你自己是电流电压数据的话,最开始通过load指令载入就是

Y

=

fft(S,N);

%做FFT变换

Ayy

=

abs(Y);

%取模

Ayy=Ayy/(N/2);

%换算成实际的幅度

Ayy(1)=Ayy(1)/2;

F=([1:N]-1)Fs/N;

%换算成实际的频率值,Fn=(n-1)Fs/N

stem(F(1:N/2),Ayy(1:N/2));

%显示换算后的FFT模值结果

title('幅度-频率曲线图');

以上就是关于使用单片机和FFT算法显示波形(高分!!!急救!!)全部的内容,包括:使用单片机和FFT算法显示波形(高分!!!急救!!)、求用MATLAB做FFT,需要傅里叶变换的数据我保存在“输出数据.txt“中,回答对的加分求完整程序、FFTW安装教程原网页等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10147108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存