适用于Octave的Matlab conv()

时间:2014-03-11 09:02:33

标签: octave

显然cconv尚未实现Octave's signal package功能。什么是重现其功能的简单方法?

想要执行的代码是:

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]')  

但尝试使用cconv()后会显示以下错误消息:

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

代码是由edX' "Discrete Time Signals and Systems"提供的分配,其中提供了集成的Matlab接口。我想尽管使用Octave。

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

答案 2 :(得分:0)

以下是进行圆形卷积的方法,该方法与ifft(fft。* fft)方法具有相同的结果。

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)]))