Thread: Struggling to Fix a Phase shift

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    4

    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);
        }
      }
    Last edited by Salem; 05-16-2017 at 01:23 AM. Reason: fixed bogus font in code section - use copy/paste as text in future

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,754
    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));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    4
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,754
    So you're all happy now?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    4
    No, the 28000 piece works, it is the rest of the shift that isn't working.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,754
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struggling with VEX programing
    By sixtysix in forum C Programming
    Replies: 1
    Last Post: 05-16-2016, 04:16 AM
  2. Shift Left, Shift Right Question
    By congi in forum C Programming
    Replies: 1
    Last Post: 01-28-2015, 10:45 AM
  3. Struggling With the Next Step.
    By Dobblaar in forum C Programming
    Replies: 8
    Last Post: 11-28-2013, 05:06 PM
  4. Replies: 11
    Last Post: 05-19-2013, 04:16 PM
  5. logical shift and arithmetic shift.(bit shifting in C )
    By A34Chris in forum C Programming
    Replies: 20
    Last Post: 09-03-2012, 11:22 AM

Tags for this Thread