- #1
nenyan
- 67
- 0
Code:
for(k=0;k<10;k++)
{
c+=shift;
dedispersion(c, displacement, fbk, result);
If I keep the code like the above, then it can be compiled and linked but it will be closed when running. I am not able to change the parameter "c" in a loop.
The whole code is followed:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358979323846264338327950288419716939937510
#define TWO_PI (6.2831853071795864769252867665590057683943L)
#define NF 33554432 //total number of data.
#define NT 512 //number of channels.
#define NTAO 32 // NTAO is the number that we use to creat filter bank.
#define FS 14000100
/* function prototypes */
void checkN (int N);
void dedispersion(double DM, int *displacement, double (*fbk)[NF/(NT*NTAO)], double (*result)[NF/(NT*NTAO)]);
int
main()
{
int i, j, k, *displacement;
double DMbase, shift, (*fbk)[NF/(NT*NTAO)], (*result)[NF/(NT*NTAO)];
double c=0.0;
FILE *fp;
char file[FILENAME_MAX]; // name of data file
DMbase=50000000000000.0;
shift=DMbase*0.1;
/*initiation */
checkN(NF); // Check that NF = 2^n for some integer n >= 1.
checkN(NT);
if(!((fbk = malloc( NF/NTAO * sizeof(double)))&&
(result = malloc( NF/NTAO * sizeof(double)))&&
(displacement = malloc( NT * sizeof(int)))))
printf("memory error \n");
/*read data from txt file.*/
if(!(fp = fopen("fbkwithdis.txt", "r")))
{
printf(" fbkwithdis.txt could not be opened!");
exit(EXIT_FAILURE);
}
for(i=0;i<NT;i++)
{
for(j=0;j<NF/(NTAO*NT);j++)
fscanf(fp, "%lf ", &fbk[i][j]);
fprintf(fp, "\n");
}
fclose(fp);
/*de-dispersion*/
for(k=0;k<10;k++)
{
c+=shift;
dedispersion(c, displacement, fbk, result);
/*output data to txt file*/
sprintf(file, "%d.txt", k);
if(!(fp = fopen(file, "w")))
{
printf(" File \'%s\' could not be opened!", file);
exit(EXIT_FAILURE);
}
for(i=0;i<NT;i++)
{
for(j=0;j<NF/(NTAO*NT);j++)
fprintf(fp, "%e ", result[i][j]);
fprintf(fp, "\n");
}
fclose(fp);
}
free(fbk);
free(result);
free(displacement);
}
/* Check that N = 2^n for some integer n >= 1. */
void
checkN (int N)
{
int i;
if(N >= 2)
{
i = N;
while(i==2*(i/2))
i = i/2; /* While i is even, factor out a 2. */
} /* For N >=2, we now have N = 2^n iff i = 1. */
if(N < 2 || i != 1)
{
printf("NF, which does not equal 2^n for an integer n >= 1.");
exit(EXIT_FAILURE);
}
}
void dedispersion(double DM, int *displacement, double (*fbk)[NF/(NT*NTAO)], double (*result)[NF/(NT*NTAO)])
{
int i, j;
int m=0, differ=0, k=0;
double time, frequency;
/*find the displacement for each channel*/
for(i=0;i<NT;i++)
{
if(i<NT/2)
frequency=(i*1.0)*FS/(NT-1);
else
frequency=(i*1.0)*FS/(NT-1)-FS;
if(frequency-0.0<0.5&&frequency-0.0>-0.5)
frequency=0.5;
time=DM/((frequency*frequency)*TWO_PI);
displacement[i]=(time/(1.0/FS))/(NT*NTAO);
}
for(i=0;i<NT;i++)
if(displacement[i]>(NF/(NTAO*NT)))
displacement[i]=NF/(NTAO*NT);
/* for(i=0;i<NT;i++)
printf("%d, ", displacement[i]);
printf("\n");
*/
/*de-dispersion*/
for(i=0;i<NT;i++)
{
for(j=0;j<NF/(NTAO*NT);j++)
{
result[i][j]=0.0;
if((i!=0)&&(i<NT/2))
differ=displacement[i-1]-displacement[i];
else if((i!=NT-1)&&(i>NT/2-1))
differ=displacement[i+1]-displacement[i];
else
differ=0;
if(differ<0)
printf("error! differ is less than zero!\n");
for(m=0;m<differ+1;m++)
{
k=j+m+displacement[i];
if(k>NF/(NTAO*NT)-1)
break;
result[i][j]+=fbk[i][k];
}
}
}
}