# Thread: Struggling to Fix a Phase shift

1. ## Struggling to Fix a Phase shift

I'm trying to figure out why my function isn't working. The data array undergoes a fit line transformation, and the data is written to a file. The next function aims to reverse the process. However, for some reason the reverse transformation is going wrong. The program reads the data file successfully, it is the transformation that is going wrong. All the variables are constants except for "t" the for loop counter. the arrays are shorts and "to", "to1", "tau", and "amp" are integers. the pointers point to shorts. I know I am missing something, I have graphed the arrays before, during, and after with no success.

Code:
``` if (ffit) {
for(int t=0;t<np;++t) {
fitvalue[t]= (data[t]-(28000+(t>=to&&t<=to1)*amp*exp((t-to/tau))));
ft=&fitvalue[t];
fwrite (ft,2,1,ffit);
}
}

// Test fit recovery
if (frecovery) {
for (int t=0; t<np; ++t) {
rcv=&recovery[t];
recovery[t]= (recovery[t]+(28000+(t>=to&&t<=to1)*amp*exp((t-to/tau))));
fwrite(rcv,2,1,frecovery);
}
}``` 2. 28000 is an awfully large offset to be applying to a signed short value, where the max positive value is only 32767. Are you losing information due to overflow and underflow?

> ft=&fitvalue[t];
> fwrite (ft,2,1,ffit);
You may as well have written
fwrite (&fitvalue[t],2,1,ffit);

Better yet, use
fwrite (&fitvalue[t],sizeof(fitvalue[t]),1,ffit);

It's not clear how you're initialising the recovery array to begin with.

Consider simplifying the expressions to make sure the round trip is working.
Eg.
fitvalue[t]= (data[t]-(28000));
...
recovery[t]= (recovery[t]+(28000)); 3. The arrays are all initialized above. The shorts all unsigned shorts so the values extend much higher, and the "round trip" is working, I just checked it this morning. I also did the simplification you suggested with the fwrite. 4. So you're all happy now? 5. No, the 28000 piece works, it is the rest of the shift that isn't working. 6. So print out as a separate value
printf("Offset at t=%d =%d\n", t, (t>=to&&t<=to1)*amp*exp((t-to/tau)));

Is it doing what you expect?
Does it result in overflow?

(t-to/tau) looks like it might need to be (t-to)/tau, judging from the boolean predicate at the start.

Do you know how to use a debugger? Popular pages Recent additions data, file, reverse, transformation, wrong 