Pregunta

Apparently the cconv function is not yet implemented for Octave's signal package. What is a simple way to reproduce its functionality?

The code which would like to execute is:

pkg load signal
n=linspace(0,1,1024);
s=sin(2.*pi.*1.*n)+.3.*sin(2.*pi.*4.*n);                  
x=s+.1.*randn(size(s));
L=100;         
y=cconv(x,ones(1,L)./L,1024);
figure,plot(y);
xlabel('n'),ylabel('y[n]'),title('Output y[n]')  

But the following error message is shown after trying to use cconv():

warning: the 'cconv' function belongs to the signal package from Octave Forge but
has not yet been implemented.

The code is an assignment provided by edX' "Discrete Time Signals and Systems" where an integrated Matlab-interface is provided. I would like to use Octave though.

¿Fue útil?

Solución

function a = cconv( b, c )

  nb = length(b) ;
  nc = length(c) ;

  ##
  ## Ensure the two vectors are the same length
  ##
  if (nb < nc)
    b = [ b zeros(1,nc-nb) ] ;
  endif

  if (nc < nb)
    c = [ c zeros(1,nb-nc) ] ;
  endif

  a = ifft(fft(b) .* fft(c)) ;

  ##
  ## Get rid of any tiny imaginary bits that should be zero
  ##
  if (all(b == real(b)) && all(c == real(c)))
    a = real(a) ;
  endif

endfunction

(source)

Otros consejos

Here's how to do circular convolution that gives identical results to the ifft(fft.*fft) method.

x=[1,2,3,4,3,2,5,0,1,-10,-1,3];

h=[0.5, 0.3, 0.15, 0.05];

Lx=length(x);

Lh=length(h);

y=zeros(1,Lx+Lh-1);

% circular convolution without zero padding and fft

y=conv([x((end-Lh+2):end) x],h);

y1=y(Lh:end-Lh+1)

% fft method for circular convolution (must zero pad to equal size)

y2=ifft(fft(x).*fft([h zeros(1,Lx-Lh)]))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top