Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,556 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,615 people online right now. Registration is fast and FREE... Join Now!




Please verify the similarity of these two codes

 
Reply to this topicStart new topic

Please verify the similarity of these two codes, matlab and c++

prads
25 Nov, 2007 - 12:03 PM
Post #1

D.I.C Head
**

Joined: 22 Oct, 2007
Posts: 111


My Contributions
Hello,
I have two codes here. Matlab code is the reference code and I have written this C++ code from that.Don’t worry about the values because i just want to know if the logic of the C++ code is same as MATLAB code coz I am getting some incorrect values as output. However I have declared a few values so that u get some clue of its datatype.
Promptcode, earlycode and latecode each has a random combination of +/- 1’s.
Cacode has 1025 random combination of +/- 1’s. Assuming that you won’t bother much on these variable values, please check the logic of the two.

Here’s the matlab code [Reference code]:
CODE

tcode       = (remCodePhase-earlyLateSpc) : ...
                          codePhaseStep : ...
                          ((blksize-1)*codePhaseStep+remCodePhase-earlyLateSpc);
            tcode2      = ceil(tcode) + 1;
            earlyCode   = caCode(tcode2);
            
            % Define index into late code vector
            tcode       = (remCodePhase+earlyLateSpc) : ...
                          codePhaseStep : ...
                          ((blksize-1)*codePhaseStep+remCodePhase+earlyLateSpc);
            tcode2      = ceil(tcode) + 1;
            lateCode    = caCode(tcode2);
            
            % Define index into prompt code vector
            tcode       = remCodePhase : ...
                          codePhaseStep : ...
                          ((blksize-1)*codePhaseStep+remCodePhase);
            tcode2      = ceil(tcode) + 1;
            promptCode  = caCode(tcode2);
            
            remCodePhase = (tcode(blksize) + codePhaseStep) - 1023.0;

%% Generate the carrier frequency to mix the signal to baseband -----------
            time    = (0:blksize) ./ settings.samplingFreq;
            
            % Get the argument to sin/cos functions
            trigarg = ((carrFreq * 2.0 * pi) .* time) + remCarrPhase;
            remCarrPhase = rem(trigarg(blksize+1), (2 * pi));
            
            % Finally compute the signal to mix the collected data to
            % bandband...baseband
            carrCos = cos(trigarg(1:blksize));
            carrSin = sin(trigarg(1:blksize));

%% Generate the six standard accumulated values ---------------------------
            % First mix to baseband
            qBasebandSignal = carrCos .* rawSignal;
            iBasebandSignal = carrSin .* rawSignal;

            % Now get early, late, and prompt values for each
            I_E = sum(earlyCode  .* iBasebandSignal);
            Q_E = sum(earlyCode  .* qBasebandSignal);
            I_P = sum(promptCode .* iBasebandSignal);
            Q_P = sum(promptCode .* qBasebandSignal);
            I_L = sum(lateCode   .* iBasebandSignal);
            Q_L = sum(lateCode   .* qBasebandSignal);



Here’s the C++ code:

CODE

long int i_ecode=0,i_lcode=0,i_pcode=0,tcode2,blksize;
int *cacode=new int[1025],i_time, i_trigarg;
int *earlycode=new int[38200],*latecode=new int[38200],*promptcode=new int[38200];  
double *carrcos=new double[38200],*carrsin=new double[38200],*ibasebandsignal=new double[38200], *qbasebandsignal=new double[38200];
double *temp1=new double[38200],*temp2=new double[38200],*temp3=new double[38200];
     double *temp4=new double[38200],*temp5=new double[38200],*temp6=new double[38200];
double i_e=0,q_e=0,i_p=0,q_p=0,i_l=0,q_l=0;
double tcode,remcodephase,earlylatespc,codephasestep, *time=new double[38200], *trigarg=new double[38200], carrfreq;

for(tcode=remcodephase-earlylatespc;tcode<=((blksize-1)*codephasestep+remcodephase-earlylatespc);)
          {      
                                                                                                                                  
            tcode2=ceil(tcode)+1;
           earlycode[i_ecode]=cacode[tcode2-1];                                  
           i_ecode+=1;
          
            tcode=tcode+codephasestep;
           }  
          
for(tcode=remcodephase+earlylatespc;tcode<=((blksize-1)*codephasestep+remcodephase+earlylatespc);)
            {      
                                                                                                                                      
            tcode2=ceil(tcode)+1;
            latecode[i_lcode]=cacode[tcode2-1];                                    
           i_lcode+=1;
          
            tcode=tcode+codephasestep;
            }  
for(tcode=remcodephase;tcode<=((blksize-1)*codephasestep+remcodephase);)
            {
                                                                                                    
            tcode2=ceil(tcode)+1;
            promptcode[i_pcode]=cacode[tcode2-1];                                    
           i_pcode+=1;
      
            tcode=tcode+codephasestep;
            
            }
                        remcodephase=tcode-1023.0;                                          
//========================Generate the carrier frequency to mix the signal to baseband===================================

for(i_time=0;i_time<=blksize;i_time++)                                     {              settings.samplingfreq=38000;
               time[i_time]= i_time/settings.samplingfreq;  
               trigarg[i_time]=((carrfreq*2*PI)*time[i_time]) + remcarrphase;
                                                                          
            }

             remcarrphase= trigarg[blksize]-((trigarg[blksize]/(2*PI)) *(2*PI));

            for(i_trigarg=0;i_trigarg<=blksize-1;i_trigarg++)  
            {
              carrcos[i_trigarg]=cos(trigarg[i_trigarg]);
              carrsin[i_trigarg]=sin(trigarg[i_trigarg]);
            }
//======================== Generate the six standard accumulated values ===================================

for(i_trigarg=0;i_trigarg<=blksize-1;i_trigarg++)  
            {
            qbasebandsignal[i_trigarg]=carrcos[i_trigarg]*rawsignal[i_trigarg];      
            ibasebandsignal[i_trigarg]=carrsin[i_trigarg]*rawsignal[i_trigarg];
            
            temp1[i_trigarg]=earlycode[i_trigarg]*ibasebandsignal[i_trigarg];
            i_e=i_e+temp1[i_trigarg];
            temp2[i_trigarg]=earlycode[i_trigarg]*qbasebandsignal[i_trigarg];
            q_e=q_e+temp2[i_trigarg];
            temp3[i_trigarg]=promptcode[i_trigarg]*ibasebandsignal[i_trigarg];
            i_p=i_p+temp3[i_trigarg];
            temp4[i_trigarg]=promptcode[i_trigarg]*qbasebandsignal[i_trigarg];
            q_p=q_p+temp4[i_trigarg];
            temp5[i_trigarg]=latecode[i_trigarg]*ibasebandsignal[i_trigarg];
            i_l=i_l+temp5[i_trigarg];
            temp6[i_trigarg]=latecode[i_trigarg]*qbasebandsignal[i_trigarg];
            q_l=q_l+temp6[i_trigarg];
            }            

Thanks,
Prads

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Please Verify The Similarity Of These Two Codes
25 Nov, 2007 - 03:15 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Your calculation of remcarrphase appears to be incorrect. In the MATLAB code, it is calculated as the remainder after division of trigarg(blksize+1) by 2*PI. In the C++ version, it is calculated as:
CODE
remcarrphase= trigarg[blksize]-((trigarg[blksize]/(2*PI)) *(2*PI))

However, if you perform an algebraic simplification of this, you wind up with:
CODE
remcarrphase= trigarg[blksize]-trigarg[blksize]
//therefore
remcarrphase=0


Based on the MATLAB code, you should be using the modulus function in the C++ version instead:
CODE
remcarrphase= fmod( trigarg[blksize], (2.0*PI) );

This returns and assigns the appropriate remainder-after-division to remcarrphase. Though given that this variable doesn't get used anywhere after that point (at least in the code you've posted), I'm not sure if that will fix your problem.

When you're debugging like this, where you know what the values SHOULD be at any given point, it's good practice to output the values of your variables (the scalar ones, at least) to the console. That way, you can compare them and see where exactly the problem is arising.

Also, it's a good idea to post a "minimal working example" code, with all of the necessary variables declared an initialized, all of the necessary headers and a main() routine, so that the other members can actually run it and see what's going on. Frequently, when you're paring down the code to the minimum amount of code required to generate the same problem, you will discover the problem yourself. Additionally, it makes it way easier on everyone else, because we either don't have to spend a lot of time getting the code to a point where it actually works, or we don't have to make a guess about what's wrong with code that doesn't actually work in the form presented (which is the case here).

Hope that helps. I can't tell if there are also other problems, but that's at least one thing tha you can check up on smile.gif

-jjh

This post has been edited by jjhaag: 25 Nov, 2007 - 03:16 PM
User is offlineProfile CardPM
+Quote Post

prads
RE: Please Verify The Similarity Of These Two Codes
25 Nov, 2007 - 05:56 PM
Post #3

D.I.C Head
**

Joined: 22 Oct, 2007
Posts: 111


My Contributions
thanks. that is one that even i had supposedly guessed. But I am still not getting the exact output and there is one or more errors. Can you please go through it once again..... crazy.gif
thanks,
prads
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Please Verify The Similarity Of These Two Codes
25 Nov, 2007 - 06:04 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Have you gone through it and performed in-depth debugging? Neither of these is complete code (variable initializations and such), so we can't run either in their current form. Go through it and insert cout << or printf statements at stategic points to output your variables to the console, and compare the output from the C++ version to what you'd get from the MATLAB code at the equivalent point in the code. This will help you narrow down where exactly the problem is, which will let you ask better and more specific questions about the code. And asking good questions is the first and most important step in getting good answers.

This post has been edited by jjhaag: 25 Nov, 2007 - 06:05 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month