hello i need c++/ matlab code of following task urgent

Discussion in 'Programmer's Corner' started by InnocentOfTheWorld, Oct 3, 2011.

  1. InnocentOfTheWorld

    Thread Starter Member

    Apr 15, 2010
    47
    1
    guys i am attaching problem....
    and please while doing coding in matlab don't use special functions like functions to find maximum amplitude out of array ....
    waiting for reply if you have time :)
     
  2. InnocentOfTheWorld

    Thread Starter Member

    Apr 15, 2010
    47
    1
    But i still want more optimized coding of c++ and also matlab guys reply


    Attaching my own solution
     
  3. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    you are more likely to get a response if you actually list the code and as I'm in a good mood at the moment I'll do it for you

    Code ( (Unknown Language)):
    1.   [FONT=&quot]#include <iostream>[/FONT]
    2.   [FONT=&quot]using namespace std;[/FONT]
    3.  
    4.   [FONT=&quot]int main()[/FONT]
    5.   [FONT=&quot]{[/FONT]
    6.   [FONT=&quot]                cout<< "*** CALCULATING AMPLITUDE, FREQUENCY AND PHASE OF A SINE WAVE ***"<<endl<<endl;[/FONT]
    7.  
    8.   [FONT=&quot]//INPUT[/FONT]
    9.   [FONT=&quot]                float a[15],amp;[/FONT]
    10.   [FONT=&quot]                int i,j;[/FONT]
    11.   [FONT=&quot]                cout<<"Input the 16 samples"<<endl<<endl;[/FONT]
    12.   [FONT=&quot]                for (i=0; i<16; i++)[/FONT]
    13.   [FONT=&quot]                {[/FONT]
    14.   [FONT=&quot]                                cin>>a[i];[/i][/FONT][i]
    15.   [FONT=&quot]                }[/FONT]
    16.  
    17.  
    18.   [FONT=&quot]//AMPLITUDE[/FONT]
    19.  
    20.   [FONT=&quot]                amp=a[0];[/FONT]
    21.   [FONT=&quot]                for (j=0; j<15; j++)[/FONT]
    22.   [FONT=&quot]                {[/FONT]
    23.   [FONT=&quot]                                if (a[j]<a[j+1])[/FONT]
    24.   [FONT=&quot]                                {[/FONT]
    25.   [FONT=&quot]                                                amp=a[j+1];[/FONT]
    26.   [FONT=&quot]                                }[/FONT]
    27.   [FONT=&quot]                }[/FONT]
    28.   [FONT=&quot]                cout<<endl<<"The amplitude is :"<<amp<<endl<<endl;[/FONT]
    29.  
    30.  
    31.   [FONT=&quot]//FREQUENCY[/FONT]
    32.  
    33.   [FONT=&quot]                int n=1;[/FONT]
    34.   [FONT=&quot]                float reg_a, reg_b, reg_c, reg_d, frequency, count=0;;[/FONT]
    35.  
    36.   [FONT=&quot]                reg_a=a[0];[/FONT]
    37.   [FONT=&quot]                reg_b=a[1];[/FONT]
    38.  
    39.   [FONT=&quot]                if(reg_a<reg_b)[/FONT]
    40.   [FONT=&quot]                {[/FONT]
    41.   [FONT=&quot]again1:[/FONT]
    42.   [FONT=&quot]                                reg_a=reg_b;[/FONT]
    43.   [FONT=&quot]                                n++;[/FONT]
    44.   [FONT=&quot]                                reg_b=a[n];[/FONT]
    45.  
    46.   [FONT=&quot]                                if (reg_b>reg_a)                                  [/FONT]
    47.   [FONT=&quot]                                                goto again1;[/FONT]
    48.   [FONT=&quot]                                else [/FONT]
    49.   [FONT=&quot]                                {[/FONT]
    50.   [FONT=&quot]                                                reg_d=reg_b;[/FONT]
    51.   [FONT=&quot]again2:[/FONT]
    52.   [FONT=&quot]                                                reg_c=reg_d;[/FONT]
    53.   [FONT=&quot]                                                n++;[/FONT]
    54.   [FONT=&quot]                                                reg_d=a[n];[/FONT]
    55.   [FONT=&quot]                                                count++;[/FONT]
    56.   [FONT=&quot]                                                if (reg_d<reg_c)[/FONT]
    57.   [FONT=&quot]                                                                goto again2;[/FONT]
    58.   [FONT=&quot]                                                else goto end;[/FONT]
    59.   [FONT=&quot]                                }[/FONT]
    60.   [FONT=&quot]                }[/FONT]
    61.   [FONT=&quot]                else[/FONT]
    62.   [FONT=&quot]                {[/FONT]
    63.   [FONT=&quot]again3:[/FONT]
    64.   [FONT=&quot]                                reg_a=reg_b;[/FONT]
    65.   [FONT=&quot]                                n++;[/FONT]
    66.   [FONT=&quot]                                reg_b=a[n];[/FONT]
    67.   [FONT=&quot]                                if (reg_b<reg_a)                                  [/FONT]
    68.   [FONT=&quot]                                                goto again3;[/FONT]
    69.   [FONT=&quot]                                else [/FONT]
    70.   [FONT=&quot]                                {[/FONT]
    71.   [FONT=&quot]                                                reg_d=reg_b;[/FONT]
    72.   [FONT=&quot]again4:[/FONT]
    73.   [FONT=&quot]                                                reg_c=reg_d;[/FONT]
    74.   [FONT=&quot]                                                n++;[/FONT]
    75.   [FONT=&quot]                                                reg_d=a[n];[/FONT]
    76.   [FONT=&quot]                                                count++;[/FONT]
    77.   [FONT=&quot]                                                if (reg_d>reg_c)[/FONT]
    78.   [FONT=&quot]                                                                goto again4;[/FONT]
    79.   [FONT=&quot]                                                else goto end;[/FONT]
    80.   [FONT=&quot]                                }[/FONT]
    81.   [FONT=&quot]                }[/FONT]
    82.  
    83.   [FONT=&quot]end:[/FONT]
    84.   [FONT=&quot]                count=count*2;[/FONT]
    85.   [FONT=&quot]                frequency=(1/(count/1000));[/FONT]
    86.   [FONT=&quot]                cout<<endl<<"The frequency is :"<<frequency<<" Hz"<<endl<<endl;[/FONT]
    87.  
    88.  
    89.   [FONT=&quot]//PHASE[/FONT]
    90.  
    91.   [FONT=&quot]                float a0, a1, a2, a3, a4, phase;[/FONT]
    92.   [FONT=&quot]                a0=amp;[/FONT]
    93.   [FONT=&quot]                a1=a[0];[/FONT]
    94.   [FONT=&quot]                a2=(a1/a0);[/FONT]
    95.   [FONT=&quot]                a3=(a2 * a2 * a2);[/FONT]
    96.   [FONT=&quot]                a4=(a3 * a2 * a2);[/FONT]
    97.   [FONT=&quot]                phase=a2 + (a3/6) + ((3*a4)/40);[/FONT]
    98.   [FONT=&quot]                cout<<endl<<"The phase is :"<<phase<<" radians"<<endl<<endl;[/FONT]
    99.  
    100.  
    101.   [FONT=&quot]                return 0;[/FONT]
    102.   [FONT=&quot]}[/FONT]
    103.   [/i]

     
  4. InnocentOfTheWorld

    Thread Starter Member

    Apr 15, 2010
    47
    1
    yes i want my this c++ code to be more optimized as a better solution.
    and also i want you guys to help me in creating matlab code as i am new on matlab anyone if have time help me and tell me how to take a start any sample code for finding anyone of the above asked tasks so that i can understand( tell me both about special functions and creating my own logic e.g maxampl(array); and finding it otherwise)
     
    Last edited: Oct 7, 2011
  5. InnocentOfTheWorld

    Thread Starter Member

    Apr 15, 2010
    47
    1
    guys i did somehow a little effort and coding in matlab although it finds out maximum amplitude but i want other parameters also to find out and return all of them.help me in improving my code.thanks.

    Code ( (Unknown Language)):
    1. function y=amplitude(array)
    2. y=array(1,1);
    3. for j=1:14
    4.     if array(1,j)<array(1,j+1)
    5.         y=array(1,j+1);
    6.     end
    7. end
    8.            
    9.  
    10.  
    11.        
    12.    
     
  6. InnocentOfTheWorld

    Thread Starter Member

    Apr 15, 2010
    47
    1
    hello i found out a code of matlab on internet. although it is much optimized but i am unable to understand it can anybody explain it to me...how it calculates amplitude and frequency????? fs is sampled frequency and x is the input of samples.
    Code ( (Unknown Language)):
    1.  
    2. function [frq,amp,phi,ave] = sinfapm(x,fs)
    3. %   Amplitude, frequency, phase  value of sampled sine wave
    4.  
    5. if nargin<2, error('Number of input arguments should be >= 2'), end
    6. N   = length(x);                %   Length of time series of x
    7. n   = 2:N-1;
    8. x   = x(:);                     %   Make x a column vector
    9. ave = mean(x);
    10. xs  = x(n-1)+x(n+1);
    11. C   = xs'*x(n)/(x(n)'*x(n))/2;
    12. frq = acos(C)*fs*.5/pi;         %   frequency
    13. if nargout>1                    %   amplitude:
    14.     amp = sqrt((x(n)'*x(n)-2*x(n)'*x(n+1)*C + x(n+1)'*x(n+1))/(1-C^2)/(N-2));
    15.     if nargout>2
    16.         phi = asin(x(1)/amp);   %   Phase approximation
    17.     end
    18. end
    19. end
     
Loading...