127 lines
2.5 KiB
C
127 lines
2.5 KiB
C
#include "string.h"
|
||
#include "stdio.h"
|
||
#include "arm_math.h"
|
||
#include "stm32f4xx.h"
|
||
|
||
#include "mymem.h"
|
||
|
||
#include "fft2d.h"
|
||
|
||
|
||
|
||
|
||
|
||
int FFT2D_Init (FFT2D_Struct *fft2d,int len)
|
||
{
|
||
fft2d->fftLen=len;
|
||
if (arm_rfft_fast_init_f32 (&fft2d->fftStruct,len)==0)
|
||
{
|
||
fft2d->imgBuff=mymalloc (len*len*sizeof (float32_t));
|
||
fft2d->columnOutBuff=mymalloc (len*sizeof (float32_t));
|
||
fft2d->rowInBuff=mymalloc (len*sizeof (float32_t));
|
||
if ((fft2d->imgBuff!=0) &&(fft2d->columnOutBuff!=0) && (fft2d->rowInBuff!=0))
|
||
return 0;
|
||
else
|
||
{
|
||
myfree(fft2d->imgBuff);
|
||
myfree(fft2d->columnOutBuff);
|
||
myfree(fft2d->rowInBuff);
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
|
||
//傅里叶变换指定图像
|
||
void FFT2D_Rfft (FFT2D_Struct *fft2d,float32_t *img,int xsize,int ysize)
|
||
{
|
||
//变换512行
|
||
for (int i=0;i<fft2d->fftLen;i++)
|
||
{
|
||
//变换一行
|
||
if (i<ysize)
|
||
{
|
||
for (int j=0;j<fft2d->fftLen;j++)
|
||
{
|
||
if (j<xsize)
|
||
{
|
||
fft2d->rowInBuff[j]=img[i*xsize+j];
|
||
}
|
||
else
|
||
{
|
||
fft2d->rowInBuff[j]=0;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
for (int j=0;j<fft2d->fftLen;j++)
|
||
{
|
||
fft2d->rowInBuff[j]=0;
|
||
}
|
||
}
|
||
arm_rfft_fast_f32 (&fft2d->fftStruct,fft2d->rowInBuff,fft2d->imgBuff+i*fft2d->fftLen,0);
|
||
}
|
||
|
||
//变换512列
|
||
for (int i=0;i<fft2d->fftLen;i++)
|
||
{
|
||
//变换一列
|
||
for (int j=0;j<fft2d->fftLen;j++)
|
||
{
|
||
fft2d->rowInBuff[j]=fft2d->imgBuff[j*fft2d->fftLen+i];
|
||
}
|
||
arm_rfft_fast_f32 (&fft2d->fftStruct,fft2d->rowInBuff,fft2d->columnOutBuff,0);
|
||
for (int j=0;j<fft2d->fftLen;j++)
|
||
{
|
||
fft2d->imgBuff[j*fft2d->fftLen+i]=fft2d->columnOutBuff[j];
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
//傅里叶逆变换,如果没有输入图像,则变换FFT2D_Struct.imgBuff 中存储的图像
|
||
void FFT2D_Rifft (FFT2D_Struct *fft2d,float32_t *img)
|
||
{
|
||
float32_t *inImg=0;
|
||
if (img) inImg=img; else inImg=fft2d->imgBuff;
|
||
//逆变换512列
|
||
for (int i=0;i<fft2d->fftLen;i++)
|
||
{
|
||
//逆变换一列
|
||
for (int j=0;j<fft2d->fftLen;j++)
|
||
{
|
||
fft2d->rowInBuff[j]=inImg[j*fft2d->fftLen+i];
|
||
}
|
||
arm_rfft_fast_f32 (&fft2d->fftStruct,fft2d->rowInBuff,fft2d->columnOutBuff,1);
|
||
for (int j=0;j<fft2d->fftLen;j++)
|
||
{
|
||
fft2d->imgBuff[j*fft2d->fftLen+i]=fft2d->columnOutBuff[j];
|
||
}
|
||
}
|
||
|
||
//逆变换512行
|
||
for (int i=0;i<fft2d->fftLen;i++)
|
||
{
|
||
//逆变换一行
|
||
for (int j=0;j<fft2d->fftLen;j++)
|
||
{
|
||
fft2d->rowInBuff[j]=fft2d->imgBuff[i*fft2d->fftLen+j];
|
||
}
|
||
arm_rfft_fast_f32 (&fft2d->fftStruct,fft2d->rowInBuff,fft2d->imgBuff+i*fft2d->fftLen,1);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
void FFT2D_Delete (FFT2D_Struct *fft2d)
|
||
{
|
||
myfree(fft2d->imgBuff);
|
||
myfree(fft2d->columnOutBuff);
|
||
myfree(fft2d->rowInBuff);
|
||
}
|
||
|
||
|
||
|
||
|