Thread: help me

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    38

    help me

    Dear all,
    I have a set of data, say x(n)={1,2,3,4} if I want to calculate
    Code:
    actually, this is not code.(but if i not type like this, the article cannot post,sorry)
    Y[0]=x(0)*x(0)+x(1)*x(1)+x(2)*x(2)+x(3)*x(3)
    Y[1]=x(0)*x(1)+x(1)*x(2)+x(2)*x(3)
    Y[2]=x(0)*x(2)+x(1)*x(3)
    Y[3]=x(0)*x(3)
    (every time, the set of data times itself by shift to left by 1,at the begining, it times itself without shifting)
    How can I program it?
    Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You lost me. Work the formulae out in words on paper (or a text editor, whatever), and then convert it to code. You'll likely need two loops to do this. For loops usually work fine for this sort of thing. I *kind of* get the idea of what you're doing, but you don't seem to have a real pattern in what you've detailed here.

    Some things are multiplied by themselves, some times they aren't. Not very clear.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    38
    Sorry, not present clearly here, oh, maybe i should say sorry for my poor english.

    Actually, this is what autocorrelation does in Digital signal processing.
    [question]
    A set of data, [X(0),x(1),x(2),x(3)]=[1,2,3,4]
    at 0, Y[0]=x(0)*x(0)+x(1)*x(1)+x(2)*x(2)+x(3)*(x(3)
    at 1, shift left by 1, that is
    ....................x(0) x(1) X(2) x(3)
    .............x(0) x(1) x(2) x(3)

    Y[1]=x(0)*x(1)+x(1)*x(2)+x(2)*x(3)

    so on....
    until
    .....................x(0) x(1) X(2) x(3)
    x(0) x(1) X(2) x(3)
    Y[3]=x(0)*x(3)
    How to program it? I have get two for loop, but it only calculates Y[0] correctly. others return me random number.
    [/question]
    Is my presentation clearly?
    Last edited by lwong; 10-21-2003 at 08:10 AM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Let's try something like this, with debugging printfs left in.
    Code:
    /* calculation description
    Y[0] = x(0) * x(0) + x(1) * x(1) + x(2) * x(2) + x(3) * x(3)
    Y[1] = x(0) * x(1) + x(1) * x(2) + x(2) * x(3)
    Y[2] = x(0) * x(2) + x(1) * x(3)
    Y[3] = x(0) * x(3)
    */
    #include <stdio.h>
    
    #define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
    
    int main(void)
    {
       int x[] = {1,2,3,4}, y [ ARRAYSIZE(x) ];
       size_t i,j;
       for(j = 0; j < ARRAYSIZE(y); ++j)
       {
          y[j] = 0;
          printf("y[%d] =", (int)j);
          for(i = j; i < ARRAYSIZE(x); ++i)
          {
             printf("%s x[%d] * x[%d]", i != j ? " +" : "", (int)(i - j), (int)i);
             y[j] += x[i - j] * x[i];
          }
          printf(" = %d\n", y[j]);
       }
       return 0;
    }
    
    /* my output
    y[0] = x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3] = 30
    y[1] = x[0] * x[1] + x[1] * x[2] + x[2] * x[3] = 20
    y[2] = x[0] * x[2] + x[1] * x[3] = 11
    y[3] = x[0] * x[3] = 4
    */
    Is this in the ballpark?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    38
    Thank you Dave_Sinkula.
    I think this is my want. try it now

Popular pages Recent additions subscribe to a feed