⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp_lab2.c

📁 c语言+ARM汇编用快速傅立叶变换实现图像压缩。FFT部分用ARM汇编实现
💻 C
字号:
#include "MP_lab2.h"


int readfile(unsigned char *a)
{
   FILE *fp;
   if((fp=fopen("D:\\my docs\\arm project\\MP_lab2\\xxx.raw","rb+"))==NULL)
   {
      printf("\nCannot open file strike any key exit!");
      return 0;
   }
   fread(a,ROW*COL,1,fp);
   fclose(fp);
   return 1;
}

double sqr(double ff)
{
	return(ff*ff);
}


/* f1, f2 swap */
void exchange(double *f1, double *f2)
{
	double ttf;
	ttf= *f1;
	*f1= *f2;
	*f2=ttf;
}

int fft(int n, double *ar, double *ai)
{
	int n2, a, c, d, f, g, h, j;
	double b, e, k, i, n1, co, si;
	int m, p, q, r, s/*count*/;
	n1=log10(n)/log10(2);
	n2=-1;
	a=n;
	b=2*PI/n;
	//count=1;
	for(c=1;c<=n1;c++)
	{
		d=a;
		a=a/2;
		e=0;
		for(f=0;f<a;f++)
		{
			co=cos(e);
			si=sin(e)*n2;
			//count++;
			e=e+b;
			for(g=d;g<=n;g+=d)
			{
				h=g-d+f;
				j=h+a;
				k=*(ar+h)-*(ar+j);
				i=*(ai+h)-*(ai+j);
				*(ar+h)=*(ar+h)+*(ar+j);
				*(ai+h)=*(ai+h)+*(ai+j);
				*(ar+j)=co*k+si*i;
				*(ai+j)=co*i-si*k;
			}
		}
		b=2*b;
	}
	//printf("%d\n",count);
	m=0;
	p=n/2;
	q=n-1;
	for(r=0;r<q;r++)
	{
		if((r-m)<0)
		{
			exchange((double *)ar+r, (double *)ar+m);
			exchange((double *)ai+r, (double *)ai+m);
		}
		s=p;
		while((s-m)<1)
		{
			m=m-s;
			s=s/2;
		}
		m=m+s;
	}
	for(s=0;s<n;s++)
	{
		*(ar+s)=*(ar+s)/n;
		*(ai+s)=*(ai+s)/n;
	}
	return 1;
}


//

//whole fft
int n_fft(unsigned char *inimage, int *prdata,int *pidata,int *ap,
				int row, int col)
{	
	int i,j;
	int *ar;
	int *ai;
	int gmax;
	ar=malloc(2*col*sizeof(int));
	ai=malloc(2*col*sizeof(int));
	for(i=0;i<row;i++)
		for(j=0;j<col;j++)
		{
			*(prdata+i*col+j)=(int)*(inimage+i*col+j);
			*(pidata+i*col+j)=0;
		}

	for(i=0;i<row;i++)
	{
		for(j=0;j<col;j++)
		{
			*(ar+j)=*(prdata+i*col+j);
			*(ai+j)=*(pidata+i*col+j);
		}
		fft1(col,ar,ai);
		for(j=0;j<col;j++)
		{
			*(prdata+i*col+j)=*(ar+j);
			*(pidata+i*col+j)=*(ai+j);
		}
	}
	for(i=0;i<col;i++)
	{
		for(j=0;j<row;j++)
		{
			*(ar+j)=*(prdata+j*col+i);
			*(ai+j)=*(pidata+j*col+i);
		}
		fft1(row,ar,ai);
		for(j=0;j<row;j++)
		{
			*(prdata+j*col+i)=*(ar+j);
			*(pidata+j*col+i)=*(ai+j);
		}
	}
	printf("the end\n");
	gmax=0;
	for(i=0;i<row;i++)
		for(j=1;j<col;j++)
		{
			*(ap+col*i+j)=sqrt(sqr(*(prdata+i*col+j))+sqr(*(pidata+i*col+j)));
			if(gmax<*(ap+col*i+j))
				gmax=*(ap+col*i+j);
		}
	for(i=0;i<row;i++)
		for(j=1;j<col;j++)
		{
			*(ap+i*col+j)=(*(ap+col*i+j)*LEVEL/gmax);
		}

	free(ar);
	free(ai);
	return 1;

}

int main()
{
  FILE *fp;
  int i;
  unsigned char inimage[ROW*COL],outimage[ROW*COL];
  int prdata[ROW*COL],pidata[ROW*COL],ap[ROW*COL];
  if (!readfile(inimage))
     printf("Read file failed!\n");
  if (n_fft(inimage,prdata,pidata,ap,ROW,COL))
     printf("FFT successed!\n");
  if((fp=fopen("D:\\my docs\\arm project\\MP_lab2\\xxx1.raw","wb+"))==NULL)
    {
    printf("\nCannot open file strike any key exit!");
    return 0;
    }
  for (i=0;i<ROW*COL;i++) 
  {
     outimage[i]=(unsigned char)ap[i];
  }
  fwrite(outimage,ROW*COL,1,fp);
  fclose(fp);
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -