/*------------------------------------------------------|
| SAR Group of Statistic signal processing laborary |
| Shanghai Jiaotong University |
| 2002-8-7 |
-------------------------------------------------------*/
/* this file and the associated header file is created by wkz*/
/* the functions in this file defined the method of use of *.mat file fomat*/
/* these functions should be used together with a file named "MATHeader" with no ext*/
#include "math.h"
#include "file_io.h"
/*------------------------------------------------------------------------------------------|
| function name: writeMATfile_data |
| remarks : write data matrix (complex data) into disk in the form of *.mat |
| input params : pdata_re -> pointer of the real part of data matrix |
| pdata_im -> pointer of the image parrt of data matrix |
| row -> the number of rows of the data matrix |
| col -> the number of columns of the data matrix |
| fn -> destination file name |
| return value : error code and 0 for no error |
| warning : this function does not check whether the size of matrix is valid |
| notation : the default name of variable is 'SAR_data' when you load the file |
| in matlab enviroment |
| SAR Group of SDSP lab 2002-8-10 By WKZ |
|__________________________________________________________________________________________*/
int writeMATfile_data(double **pdata_re,double **pdata_im,unsigned long row,unsigned long col,char *fn)
{
FILE *pfile=fopen(fn,"wb");//以只写方式打开一个二进制文件,文件不存在时建立新文件,文件存在时清空
unsigned long temp[11];
unsigned long i,j;
char VaribleName[]="SAR_data";
FILE *pheader;
char header[0x90];
if(pfile==NULL)
return 2;
pheader=fopen("MATHeader","rb");//以只读方式打开一个二进制文件
if(pheader==NULL)
{
/* printf("Can not open the MAT Header file");*/
return 1;
}
fread(header,0x84,1,pheader);//从pheader中读取一个长度为0x84的数据项放在header所指的字符数组中
fclose(pheader);
fwrite(header,0x84,1,pfile);
temp[0]=row*col*8*2; /*data length*/
temp[0]+=8; /*dimension indicator*/
temp[0]+=8; /*indicator of varible*/
temp[0]+=8; /*varible name as "Wavedata"*/
temp[0]+=24; /*fixed length of something of matlab*/
temp[0]+=16; /*two times size indicator*/
temp[1]=6;temp[2]=8;temp[3]=0x806;temp[4]=0;temp[5]=5;temp[6]=8;
temp[7]=row;
temp[8]=col;
temp[9]=1;
temp[10]=8;
fwrite(temp,44,1,pfile);
fwrite(VaribleName,8,1,pfile);
temp[0]=9;temp[1]=row*col*8;
fwrite(temp,8,1,pfile);
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
fwrite(&(pdata_re[j][i]),sizeof(double),1,pfile);
}
}
temp[0]=9;temp[1]=row*col*8;
fwrite(temp,8,1,pfile);
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
/* double t=;*/
fwrite(&(pdata_im[j][i]),sizeof(double),1,pfile);
}
}
fclose(pfile);
return 0;
}
/* Error code:*/
/* ->1 Can not find or open the MAT file header. The header file named "MATHeader" with no ext,which should be used together with the programme.*/
/* ->2 Can not open and create MAT data file.*/
/*------------------------------------------------------------------------------------------------------|
| function name: x_readMATfile_data |
| remarks : read data matrix (complex data) from *.mat file on the disk |
| input params : pdata_re -> the address of pointer to the real part of 2-D complex data matrix |
| pdata_im -> the address of pointer to the image part of 2-D complex data matrix |
| row -> the address of number of rows of the data matrix |
| col -> the address of number of columns of the data matrix |
| fn -> source data file name |
| return value : error code and 0 for no error |
| warning : the function allocates the buffer of data on the heap and gives the pointers |
| of the buffer to the parameter pdata_re & pdata_im, these pointers should |
| be freed after use |
| SAR Group of SDSP lab 2002-8-10 By WKZ |
|______________________________________________________________________________________________________*/
void readMATfile_data(double ***pdata_re,double ***pdata_im,long *row,long *col,char *fn,int *error)
{
FILE *pfile=fopen(fn,"rb");
char header[0x90];
long temp[11];
double tempdata=0;
long i,j;
if(pfile==NULL)
{
*row=0;
*col=0;
if(error!=NULL)
*error=2;
return;
}
/* Adjust the file pointer to the begin of data position and get the data block size*/
fread(header,sizeof(char),0x84,pfile);
fread(temp,sizeof(long),11,pfile);
fread(header,sizeof(char),8,pfile);
fread(temp,sizeof(long),2,pfile);
*row=temp[7];
*col=temp[8];
*pdata_re=(double**)calloc(*row,sizeof(double *));
*pdata_im=(double**)calloc(*row,sizeof(double *));
if((*pdata_re==NULL)||(*pdata_im==NULL))
{
fclose(pfile);
*row=0;
*col=0;
if(error!=NULL)
*error=1;
return;
}
/* Read the real part of data block*/
for(i=0;i<*col;i++)
{
*pdata_re[i]=(double*)(calloc(*col,sizeof(double)));
for(j=0;j<*row;j++)
{
fread(&tempdata,sizeof(double),1,pfile);
*pdata_re[j][i]=tempdata;
}
}
fread(temp,sizeof(long),2,pfile);
/* read the image part of data block*/
for(i=0;i<*col;i++)
{
*pdata_im[i]=(double*)(calloc(*col,sizeof(double)));
for(j=0;j<*row;j++)
{
fread(&tempdata,sizeof(double),1,pfile);
*pdata_im[j][i]=tempdata;
}
}
fclose(pfile);
return;
}
/* Error code:*/
/* ->1 Can not allocate enough memory for data block*/
/* ->2 Can not open source data file*/
/*------------------------------------------------------------------------------------------|
| function name: x_writeMATfile |
| remarks : write data matrix (complex data) into disk in the form of *.mat |
| input params : pdata -> pointer of data matrix |
| row -> the number of rows of the data matrix |
| col -> the number of columns of the data matrix |
| fn -> destination file name |
| return value : error code and 0 for no error |
| warning : this function does not check whether the size of matrix is valid |
| notation : the default name of variable is 'SAR_data' when you load the file |
| in matlab enviroment |
| SAR Group of SDSP lab 2002-8-10 By WKZ |
|__________________________________________________________________________________________*/
int x_writeMATfile(clx **pdata,long row,long col,char *fn)
{
long i,j;
double **pdata_re=(double**)calloc(row,sizeof(double *));
double **pdata_im=(double**)calloc(row,sizeof(double *));
int error=0;
for(i=0;i<row;i++)
{
pdata_re[i]=(double *)calloc(col,sizeof(double));
pdata_im[i]=(double *)calloc(col,sizeof(double));
for(j=0;j<col;j++)
{
pdata_re[i][j]=pdata[i][j].re;
pdata_im[i][j]=pdata[i][j].im;
}
}
error=writeMATfile_data(pdata_re,pdata_im,row,col,fn);
free_mem(pdata_re,row);
free_mem(pdata_im,row);
return error;
}
/*------------------------------------------------------------------------------------------|
| function name: x_readMATfile |
| remarks : read data matrix (complex data) from *.mat file on the disk |
| input params : pdata -> the address of pointer of 2-D complex data matrix |
| row -> the address of number of rows of the data matrix |
| col -> the address of number of columns of the data matrix |
| fn -> source data file name |
| return value : error code and 0 for no error |
| warning : the function allocates the buffer of data on the heap and gives |
| the pointer of the buffer to the parameter pdata, this pointer |
| should be freed after use