Forums

Question: Autocorrelation of a sinusoidal signal:

Started by ellie October 18, 2010
Hi, I'm looking for a "for loop" code in MATLAB to obtain the normalized
autocorrelation of a sinusoidal signal that is sampled at 1000Hz and
n(number of samples taken)=200. I also need to plot it. 
Input: 
x= sin((2.*pi.*n)./5); 


I have the following but I get error in MATLAB : 

size=1:length(n)
    for  i=0:size
      
for j=1:size-i
    rxx=sum(x3(j).*x3(j+i));
end


    end 



Thank you. 


On Oct 18, 7:33&#2013266080;am, "ellie" <elnaz_am@n_o_s_p_a_m.hotmail.com> wrote:
> Hi, I'm looking for a "for loop" code in MATLAB to obtain the normalized > autocorrelation of a sinusoidal signal that is sampled at 1000Hz and > n(number of samples taken)=200. I also need to plot it. > Input: > x= sin((2.*pi.*n)./5); > > I have the following but I get error in MATLAB : > > size=1:length(n) > &#2013266080; &#2013266080; for &#2013266080;i=0:size > > for j=1:size-i > &#2013266080; &#2013266080; rxx=sum(x3(j).*x3(j+i)); > end > > &#2013266080; &#2013266080; end > > Thank you.
y = xcorr(x,x,'coeff');plot(y);
On Oct 18, 7:33&#2013266080;am, "ellie" <elnaz_am@n_o_s_p_a_m.hotmail.com> wrote:
> Hi, I'm looking for a "for loop" code in MATLAB to obtain the normalized > autocorrelation of a sinusoidal signal that is sampled at 1000Hz and > n(number of samples taken)=200. I also need to plot it. > Input: > x= sin((2.*pi.*n)./5); > > I have the following but I get error in MATLAB : > > size=1:length(n) > &#2013266080; &#2013266080; for &#2013266080;i=0:size > > for j=1:size-i > &#2013266080; &#2013266080; rxx=sum(x3(j).*x3(j+i)); > end > > &#2013266080; &#2013266080; end > > Thank you.
Do you know what your error is? Pretty simple code to ask for help on without at least making it run first. Dirk
On 10/18/2010 04:33 AM, ellie wrote:
> Hi, I'm looking for a "for loop" code in MATLAB to obtain the normalized > autocorrelation of a sinusoidal signal that is sampled at 1000Hz and > n(number of samples taken)=200. I also need to plot it. > Input: > x= sin((2.*pi.*n)./5); > > > I have the following but I get error in MATLAB : > > size=1:length(n) > for i=0:size > > for j=1:size-i > rxx=sum(x3(j).*x3(j+i)); > end > > > end
As mentioned, if you're getting an error it's exceedingly helpful to share it. In this case, though, I'm pretty sure that the error is something about a type mismatch, or the 'size' variable not being right or whatever. I see three errors in your code: First, 'size' is (if I'm not mistaken) the name of a function in Matlab. I'm not entirely sure, but I think that by assigning size = 1:length(n) you are either redefining 'size' as an array, or you're trying to shove a value where a value can't be shoved -- without knowing the error that Matlab is giving you, I can't say. Second, assuming that size is now a vector, the line for i=0:size will make Matlab cranky. You can't say (scaler):(vector) in Matlab. You can only say (scaler):(scaler), and the answer is a vector. Third, for j=1:size-i -- same problem. Try again, read the error messages, see if you can figure out what you've done wrong from the error messages, and if you do post questions _include_ the error messages. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
On Oct 18, 10:32&#2013266080;am, Tim Wescott <t...@seemywebsite.com> wrote:
> On 10/18/2010 04:33 AM, ellie wrote: > > > > > > > Hi, I'm looking for a "for loop" code in MATLAB to obtain the normalized > > autocorrelation of a sinusoidal signal that is sampled at 1000Hz and > > n(number of samples taken)=200. I also need to plot it. > > Input: > > x= sin((2.*pi.*n)./5); > > > I have the following but I get error in MATLAB : > > > size=1:length(n) > > &#2013266080; &#2013266080; &#2013266080;for &#2013266080;i=0:size > > > for j=1:size-i > > &#2013266080; &#2013266080; &#2013266080;rxx=sum(x3(j).*x3(j+i)); > > end > > > &#2013266080; &#2013266080; &#2013266080;end > > As mentioned, if you're getting an error it's exceedingly helpful to > share it. &#2013266080;In this case, though, I'm pretty sure that the error is > something about a type mismatch, or the 'size' variable not being right > or whatever. > > I see three errors in your code: > > First, 'size' is (if I'm not mistaken) the name of a function in Matlab. > &#2013266080; I'm not entirely sure, but I think that by assigning > > size = 1:length(n) > > you are either redefining 'size' as an array, or you're trying to shove > a value where a value can't be shoved -- without knowing the error that > Matlab is giving you, I can't say. > > Second, assuming that size is now a vector, the line > > &#2013266080; &#2013266080;for i=0:size > > will make Matlab cranky. &#2013266080;You can't say (scaler):(vector) in Matlab. > You can only say (scaler):(scaler), and the answer is a vector. > > Third, for j=1:size-i -- same problem. > > Try again, read the error messages, see if you can figure out what > you've done wrong from the error messages, and if you do post questions > _include_ the error messages. > > -- > > Tim Wescott > Wescott Design Serviceshttp://www.wescottdesign.com > > Do you need to implement control loops in software? > "Applied Control Theory for Embedded Systems" was written for you. > See details athttp://www.wescottdesign.com/actfes/actfes.html- Hide quoted text - > > - Show quoted text -
Tim, Although (s)he has the correct format for the _for_ loops, (her)his problem really is not having an understanding of Matlab. For example, the vector x needs to be of the form x = sin(2*pi*f*t), where f is the frequency of the sinusoid, and t is a time vector. If (s)he wants _n_ elements in the sin vector, then (s)he needs to have somethinbg like t = 0:1/fs:n-1/fs, where fs is the sample frequency. he dosen't need the .* or ./ notation. Ellie, go read the manual! But, the basic problem is not learning Matlab. Maurice Givens
On Oct 18, 6:33&#2013266080;am, "ellie" <elnaz_am@n_o_s_p_a_m.hotmail.com> wrote:
> Hi, I'm looking for a "for loop" code in MATLAB to obtain the normalized > autocorrelation of a sinusoidal signal that is sampled at 1000Hz and > n(number of samples taken)=200. I also need to plot it. > Input: > x= sin((2.*pi.*n)./5); > > I have the following but I get error in MATLAB : > > size=1:length(n) > &#2013266080; &#2013266080; for &#2013266080;i=0:size > > for j=1:size-i > &#2013266080; &#2013266080; rxx=sum(x3(j).*x3(j+i)); > end > > &#2013266080; &#2013266080; end > > Thank you.
Ellie, I don't want you to get the idea that my previous post listed all your problems. You need to review your code logic and the autocorrelation function. You also have at least 2 other basic Matlab problems. Maurice Givens
On 10/18/2010 11:49 AM, maury wrote:
> On Oct 18, 10:32 am, Tim Wescott<t...@seemywebsite.com> wrote: >> On 10/18/2010 04:33 AM, ellie wrote: >> >> >> >> >> >>> Hi, I'm looking for a "for loop" code in MATLAB to obtain the normalized >>> autocorrelation of a sinusoidal signal that is sampled at 1000Hz and >>> n(number of samples taken)=200. I also need to plot it. >>> Input: >>> x= sin((2.*pi.*n)./5); >> >>> I have the following but I get error in MATLAB : >> >>> size=1:length(n) >>> for i=0:size >> >>> for j=1:size-i >>> rxx=sum(x3(j).*x3(j+i)); >>> end >> >>> end >> >> As mentioned, if you're getting an error it's exceedingly helpful to >> share it. In this case, though, I'm pretty sure that the error is >> something about a type mismatch, or the 'size' variable not being right >> or whatever. >> >> I see three errors in your code: >> >> First, 'size' is (if I'm not mistaken) the name of a function in Matlab. >> I'm not entirely sure, but I think that by assigning >> >> size = 1:length(n) >> >> you are either redefining 'size' as an array, or you're trying to shove >> a value where a value can't be shoved -- without knowing the error that >> Matlab is giving you, I can't say. >> >> Second, assuming that size is now a vector, the line >> >> for i=0:size >> >> will make Matlab cranky. You can't say (scaler):(vector) in Matlab. >> You can only say (scaler):(scaler), and the answer is a vector. >> >> Third, for j=1:size-i -- same problem. >> >> Try again, read the error messages, see if you can figure out what >> you've done wrong from the error messages, and if you do post questions >> _include_ the error messages. >> >> -- >> >> Tim Wescott >> Wescott Design Serviceshttp://www.wescottdesign.com >> >> Do you need to implement control loops in software? >> "Applied Control Theory for Embedded Systems" was written for you. >> See details athttp://www.wescottdesign.com/actfes/actfes.html- Hide quoted text - >> >> - Show quoted text - > > Tim, > Although (s)he has the correct format for the _for_ loops, (her)his > problem really is not having an understanding of Matlab. > For example, the vector x needs to be of the form x = sin(2*pi*f*t), > where f is the frequency of the sinusoid, and t is a time vector. If > (s)he wants _n_ elements in the sin vector, then (s)he needs to have > somethinbg like t = 0:1/fs:n-1/fs, where fs is the sample frequency. > he dosen't need the .* or ./ notation. > > Ellie, go read the manual! > > But, the basic problem is not learning Matlab.
The _format_ is correct, but unless I've forgotten all my Matlab basics (I'm a Scilab user) saying "0:size" when size is a vector will mess you up. Saying "for n = size" when size is a vector is perfectly all right. I'm suspecting that (s)he has jumped from the math book to Matlab too soon, but that's me. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
On 10/18/2010 12:03 PM, maury wrote:
> On Oct 18, 6:33 am, "ellie"<elnaz_am@n_o_s_p_a_m.hotmail.com> wrote: >> Hi, I'm looking for a "for loop" code in MATLAB to obtain the normalized >> autocorrelation of a sinusoidal signal that is sampled at 1000Hz and >> n(number of samples taken)=200. I also need to plot it. >> Input: >> x= sin((2.*pi.*n)./5); >> >> I have the following but I get error in MATLAB : >> >> size=1:length(n) >> for i=0:size >> >> for j=1:size-i >> rxx=sum(x3(j).*x3(j+i)); >> end >> >> end >> >> Thank you. > > Ellie, > I don't want you to get the idea that my previous post listed all your > problems. You need to review your code logic and the autocorrelation > function. You also have at least 2 other basic Matlab problems.
A suggestion: start really small. Like, make a few "for" loops and see how to make them work. Try a for loop that'll step through 0, 1, 2, .... Then one that'll step through -10,-1,8,17 (how am I doing that?). Then one that'll step through an arbitrary vector of numbers. Go from there. Put things together in teeny bits. Test each bit. HTH. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
On Oct 18, 2:21&#2013266080;pm, Tim Wescott <t...@seemywebsite.com> wrote:
> On 10/18/2010 11:49 AM, maury wrote: > > > > > > > On Oct 18, 10:32 am, Tim Wescott<t...@seemywebsite.com> &#2013266080;wrote: > >> On 10/18/2010 04:33 AM, ellie wrote: > > >>> Hi, I'm looking for a "for loop" code in MATLAB to obtain the normalized > >>> autocorrelation of a sinusoidal signal that is sampled at 1000Hz and > >>> n(number of samples taken)=200. I also need to plot it. > >>> Input: > >>> x= sin((2.*pi.*n)./5); > > >>> I have the following but I get error in MATLAB : > > >>> size=1:length(n) > >>> &#2013266080; &#2013266080; &#2013266080; for &#2013266080;i=0:size > > >>> for j=1:size-i > >>> &#2013266080; &#2013266080; &#2013266080; rxx=sum(x3(j).*x3(j+i)); > >>> end > > >>> &#2013266080; &#2013266080; &#2013266080; end > > >> As mentioned, if you're getting an error it's exceedingly helpful to > >> share it. &#2013266080;In this case, though, I'm pretty sure that the error is > >> something about a type mismatch, or the 'size' variable not being right > >> or whatever. > > >> I see three errors in your code: > > >> First, 'size' is (if I'm not mistaken) the name of a function in Matlab. > >> &#2013266080; &#2013266080;I'm not entirely sure, but I think that by assigning > > >> size = 1:length(n) > > >> you are either redefining 'size' as an array, or you're trying to shove > >> a value where a value can't be shoved -- without knowing the error that > >> Matlab is giving you, I can't say. > > >> Second, assuming that size is now a vector, the line > > >> &#2013266080; &#2013266080; for i=0:size > > >> will make Matlab cranky. &#2013266080;You can't say (scaler):(vector) in Matlab. > >> You can only say (scaler):(scaler), and the answer is a vector. > > >> Third, for j=1:size-i -- same problem. > > >> Try again, read the error messages, see if you can figure out what > >> you've done wrong from the error messages, and if you do post questions > >> _include_ the error messages. > > >> -- > > >> Tim Wescott > >> Wescott Design Serviceshttp://www.wescottdesign.com > > >> Do you need to implement control loops in software? > >> "Applied Control Theory for Embedded Systems" was written for you. > >> See details athttp://www.wescottdesign.com/actfes/actfes.html-Hide quoted text - > > >> - Show quoted text - > > > Tim, > > Although (s)he has the correct format for the _for_ loops, (her)his > > problem really is not having an understanding of Matlab. > > For example, the vector x needs to be of the form x = sin(2*pi*f*t), > > where f is the frequency of the sinusoid, and t is a time vector. If > > (s)he wants _n_ elements in the sin vector, then (s)he needs to have > > somethinbg like t = 0:1/fs:n-1/fs, where fs is the sample frequency. > > he dosen't need the .* or ./ notation. > > > Ellie, go read the manual! > > > But, the basic problem is not learning Matlab. > > The _format_ is correct, but unless I've forgotten all my Matlab basics > (I'm a Scilab user) saying "0:size" when size is a vector will mess you > up. &#2013266080;Saying "for n = size" when size is a vector is perfectly all right. > > I'm suspecting that (s)he has jumped from the math book to Matlab too > soon, but that's me. > > -- > > Tim Wescott > Wescott Design Serviceshttp://www.wescottdesign.com > > Do you need to implement control loops in software? > "Applied Control Theory for Embedded Systems" was written for you. > See details athttp://www.wescottdesign.com/actfes/actfes.html- Hide quoted text - > > - Show quoted text -
You're absolutly right Tim, I missed that (s)he made size a nth-order vector.