Re: Incompatibility MATLAB-OCTAVE : xcorr.m

View: New views
5 Messages — Rating Filter:   Alert me  

Parent Message unknown Re: Incompatibility MATLAB-OCTAVE : xcorr.m

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Oct 21, 2009 at 6:49 AM, Pierre Lonchampt
<plonchampt@...> wrote:

> Hi
>
> I am currently trying to port an algorithm written for Matlab into Octave.
> On top of the usual and already documented discrepancies, I stumbled against
> the following one which took me a while to identify:
>
> When calling
>
> function [R, lags] = xcorr (X, Y, maxlag, scale)
>
> In octave, having maxlag longer than X returns an error message.
> In Matlab, that is perfectly OK and zeros are padded around of the
> correlation "center" when that is the case (and which is true
> mathematically-wise).
>
> I quickly tweaked the octave script in a ugly way to work "FOR MY
> APPLICATION ONLY". I attach it nevertheless as it may illustrate my point
> better than my words above. The tweak is between the comments starting with
> %PL.
>
> I am not sure whether you guys will consider this discrepancy to be a bug in
> either side, but in all cases it could be documented better somewhere.
>
> Regards and congrats for the great tool octave is!
>
> PS: this is true in both versions 3.0.1 and 3.2.3 compared with both matlab
> R11 and R12 (I don't know about more recent releases, nevertheless Mathworks
> documentation does not refer to Maxlag needing to be shorter than the input
> vetcor(s)...
>
> Hope it helps
>

There is no xcorr function in Octave (signal is an external package)
-> you're on the wrong list. I'm forwarding this to OctaveForge.

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Incompatibility MATLAB-OCTAVE : xcorr.m

by Søren Hauberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

ons, 21 10 2009 kl. 13:12 +0200, skrev Jaroslav Hajek:
> > When calling
> >
> > function [R, lags] = xcorr (X, Y, maxlag, scale)
> >
> > In octave, having maxlag longer than X returns an error message.
> > In Matlab, that is perfectly OK and zeros are padded around of the
> > correlation "center" when that is the case (and which is true
> > mathematically-wise).

I think the attached patch fixes this, but since you didn't provide an
example I'm not sure. It would be nice if you could possibly test this.

Thanks,
Søren

[xcorr.patch]

Index: xcorr.m
===================================================================
--- xcorr.m (revision 6347)
+++ xcorr.m (working copy)
@@ -127,8 +127,12 @@
   if !isscalar(maxlag) && !isempty(maxlag)
     error("xcorr: maxlag must be a scalar");
   endif
-  if maxlag>N-1,
-    error("xcorr: maxlag must be less than length(X)");
+  if (maxlag > N-1)
+    pad_result = maxlag - (N - 1);
+    maxlag = N - 1;
+    %error("xcorr: maxlag must be less than length(X)");
+  else
+    pad_result = 0;
   endif
   if isvector(X) && isvector(Y) && length(X) != length(Y) && \
  !strcmp(scale,'none')
@@ -204,8 +208,12 @@
     R = R.';
   endif
   
+  ## Pad result if necesary
+  R = [zeros(1, pad_result), R, zeros(1, pad_result)];
+  
   ## return the lag indices if desired
   if nargout == 2
+    maxlag += pad_result;
     lags = [-maxlag:maxlag];
   endif
 


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Parent Message unknown Re: Incompatibility MATLAB-OCTAVE : xcorr.m

by Søren Hauberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

fre, 23 10 2009 kl. 10:43 +1100, skrev Pierre Lonchampt:

> Thanks a lot Soren, apologies for my incomplete reporting;
> Your patch works for my application indeed.
>
> an example being:
>
> octave:22> x=rand(1,60)
> octave:23> y=xcorr(x,70,'coeff')
> octave:24> size(y)
> ans =
>
>      1   141
>
> which was giving previously an error...now giving a result similar to
> Matlab

Okay, thanks.

> Pushing it a bit further, I just found some other minor discrepancies:
>
> Octave 3.0.1 (with the xcorr+your patch)
> octave:14> xcorr(ones(1,2),ones(1,3),2)
> ans =
>
>    1.0000e+00   2.0000e+00   2.0000e+00   1.0000e+00   1.3878e-17
>
> MAtlabR11:
> » xcorr(ones(1,2),ones(1,3),2)
>
> ans =
>
>          0    1.0000    2.0000    2.0000    1.0000

I'd like to fix this but I am unsure if I can. See below.

> I attach the matlab xcorr.m file if that can help.

NEVER EVER EVER do this! This code belongs to Mathworks and not to you,
so you don't have the rights to distribute it. By distributing this file
you are violating Mathworks copyright. While I did not look at the code
you sent me off-list, I am unsure if I can look into the problem any
more. I don't want the Octave-Forge project to get legal issues, so I
would prefer if someone else could look into the reported problem.

Søren


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Incompatibility MATLAB-OCTAVE : xcorr.m

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Oct 23, 2009 at 9:39 AM, Søren Hauberg <soren@...> wrote:

> fre, 23 10 2009 kl. 10:43 +1100, skrev Pierre Lonchampt:
>> Thanks a lot Soren, apologies for my incomplete reporting;
>> Your patch works for my application indeed.
>>
>> an example being:
>>
>> octave:22> x=rand(1,60)
>> octave:23> y=xcorr(x,70,'coeff')
>> octave:24> size(y)
>> ans =
>>
>>      1   141
>>
>> which was giving previously an error...now giving a result similar to
>> Matlab
>
> Okay, thanks.
>
>> Pushing it a bit further, I just found some other minor discrepancies:
>>
>> Octave 3.0.1 (with the xcorr+your patch)
>> octave:14> xcorr(ones(1,2),ones(1,3),2)
>> ans =
>>
>>    1.0000e+00   2.0000e+00   2.0000e+00   1.0000e+00   1.3878e-17
>>
>> MAtlabR11:
>> » xcorr(ones(1,2),ones(1,3),2)
>>
>> ans =
>>
>>          0    1.0000    2.0000    2.0000    1.0000
>
> I'd like to fix this but I am unsure if I can. See below.
>
>> I attach the matlab xcorr.m file if that can help.
>
> NEVER EVER EVER do this! This code belongs to Mathworks and not to you,
> so you don't have the rights to distribute it. By distributing this file
> you are violating Mathworks copyright. While I did not look at the code
> you sent me off-list, I am unsure if I can look into the problem any
> more.

I think you're overreacting a bit, Soren. In fact, even if you did
look, no law does prohibit reading, only copying. If you inspect a
m-file from Matlab to learn just the trick, and don't actually
memorize and reproduce any code, it's technically still legal.
But in any case, you said you didn't look, so you didn't look and that
settles it. Innocent until proved guilty.

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev

Re: Incompatibility MATLAB-OCTAVE : xcorr.m

by Søren Hauberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

fre, 23 10 2009 kl. 13:02 +0200, skrev Jaroslav Hajek:
> I think you're overreacting a bit, Soren.

Yeah, you're probably right. I tend to be a bit sensitive about issues
such as this. While I don't agree with Mathwork's choice of
distribution, I do have a great deal of respect for their choice.

I also really want to send the message that nobody should be
distributing Mathwork's code to Octave developers.

Anyway, I probably overreacted, and for that I apologize,
Søren


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Octave-dev mailing list
Octave-dev@...
https://lists.sourceforge.net/lists/listinfo/octave-dev