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

#### InnocentOfTheWorld

Joined Apr 15, 2010
47
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

#### Attachments

• 182 KB Views: 13

#### InnocentOfTheWorld

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

Attaching my own solution

#### Attachments

• 313.5 KB Views: 15

#### DumboFixer

Joined Feb 10, 2009
217
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

Rich (BB code):
  #include <iostream>
using namespace std;

int main()
{
cout<< "*** CALCULATING AMPLITUDE, FREQUENCY AND PHASE OF A SINE WAVE ***"<<endl<<endl;

//INPUT
float a[15],amp;
int i,j;
cout<<"Input the 16 samples"<<endl<<endl;
for (i=0; i<16; i++)
{
cin>>a;
}

//AMPLITUDE

amp=a[0];
for (j=0; j<15; j++)
{
if (a[j]<a[j+1])
{
amp=a[j+1];
}
}
cout<<endl<<"The amplitude is :"<<amp<<endl<<endl;

//FREQUENCY

int n=1;
float reg_a, reg_b, reg_c, reg_d, frequency, count=0;;

reg_a=a[0];
reg_b=a[1];

if(reg_a<reg_b)
{
again1:
reg_a=reg_b;
n++;
reg_b=a[n];

if (reg_b>reg_a)
goto again1;
else
{
reg_d=reg_b;
again2:
reg_c=reg_d;
n++;
reg_d=a[n];
count++;
if (reg_d<reg_c)
goto again2;
else goto end;
}
}
else
{
again3:
reg_a=reg_b;
n++;
reg_b=a[n];
if (reg_b<reg_a)
goto again3;
else
{
reg_d=reg_b;
again4:
reg_c=reg_d;
n++;
reg_d=a[n];
count++;
if (reg_d>reg_c)
goto again4;
else goto end;
}
}

end:
count=count*2;
frequency=(1/(count/1000));
cout<<endl<<"The frequency is :"<<frequency<<" Hz"<<endl<<endl;

//PHASE

float a0, a1, a2, a3, a4, phase;
a0=amp;
a1=a[0];
a2=(a1/a0);
a3=(a2 * a2 * a2);
a4=(a3 * a2 * a2);
phase=a2 + (a3/6) + ((3*a4)/40);

return 0;
}


#### InnocentOfTheWorld

Joined Apr 15, 2010
47
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:

#### InnocentOfTheWorld

Joined Apr 15, 2010
47
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.

Rich (BB code):
function y=amplitude(array)
y=array(1,1);
for j=1:14
if array(1,j)<array(1,j+1)
y=array(1,j+1);
end
end

#### InnocentOfTheWorld

Joined Apr 15, 2010
47
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.
Rich (BB code):
function [frq,amp,phi,ave] = sinfapm(x,fs)
%   Amplitude, frequency, phase  value of sampled sine wave

if nargin<2, error('Number of input arguments should be >= 2'), end
N   = length(x);                %   Length of time series of x
n   = 2:N-1;
x   = x(:);                     %   Make x a column vector
ave = mean(x);
xs  = x(n-1)+x(n+1);
C   = xs'*x(n)/(x(n)'*x(n))/2;
frq = acos(C)*fs*.5/pi;         %   frequency
if nargout>1                    %   amplitude:
amp = sqrt((x(n)'*x(n)-2*x(n)'*x(n+1)*C + x(n+1)'*x(n+1))/(1-C^2)/(N-2));
if nargout>2
phi = asin(x(1)/amp);   %   Phase approximation
end
end
end