help-octave
[Top][All Lists]

## Re: Help to vectorize THIS loop

 From: Ben Abbott Subject: Re: Help to vectorize THIS loop Date: Wed, 02 Oct 2013 19:38:14 -0400

```On Sep 25, 2013, at 12:21 PM, Macy wrote:

> Anybody have any comments on this one?
>
> Compare these two, which seems like they should yield similar results:
> [watch for wrap]
>
> CLOCKJITTERCOMPARE.m
> %  these values are 'kind of' like what is being used
> SP=20e3;
> SR=200e6;
> t=([1:SP]-1)/SR;
> sig=.99*cos(2*pi()*t*20e6);
> clkjtr=200e-15;
>
> % actual code
> dtn=clkjtr*randn(1,SP);
> tb=t+dtn;
>
> sigcn1=sig;
> for i=2:(SP-1)
>  if (tb(i)<t(i))
>    sigcn1(i)=sig(i)-SR*(sig(i)-sig(i-1))*(t(i)-tb(i));
>  else
>    sigcn1(i)=sig(i)+SR*(sig(i+1)-sig(i))*(tb(i)-t(i));
>  endif
> endfor
> %  end points, note t(1) == 0
>  sigcn1(1)=sig(1)+SR*(sig(2)-sig(1))*tb(1);
> %
>  sigcn1(end)=sig(end)+SR*(sig(end)-sig(end-1))*(tb(end)-t(end));
>
> %  save original value
> dtno=dtn;
> dtn=SR*dtno;
> %
>
> sigcn2=sig;
> idxp=dtn>0;idxp(1)=0;idxp(end)=0;
> idxn=dtn<0;idxn(1)=0;idxn(end)=0;
>
> dsigup=[0,diff(sig(2:end)),0];
> dsigdn=[0,diff(sig)];
> sigcn2=sig+(dsigup.*idxp+dsigdn.*idxn).*dtn;
> sigcn2(1)=sig(1)+(sig(2)-sig(1))*dtn(1);
> sigcn2(end)=sig(end)+(sig(end)-sig(end-1))*dtn(end);
>
> %  restore original value
> dtn=dtno;
>
> plot(sigcn2-sigcn1);
> % shouldn't this be a 'zero' line?

I'm not sure you'll get much help tracking down numeric noise.

Ben

```