diff options
author | Sunil Shetye | 2018-07-25 17:11:09 +0530 |
---|---|---|
committer | Sunil Shetye | 2018-07-26 23:50:17 +0530 |
commit | 1251f70aa3442736ce6fd9c4fb7fbce412af5a52 (patch) | |
tree | 360311ffaf6151c5066439f481e8ac38cfd047b9 /macros/cconv.sci | |
parent | 9ca7882cee16ad48b18df989e8300c697010e55a (diff) | |
download | FOSSEE-Signal-Processing-Toolbox-1251f70aa3442736ce6fd9c4fb7fbce412af5a52.tar.gz FOSSEE-Signal-Processing-Toolbox-1251f70aa3442736ce6fd9c4fb7fbce412af5a52.tar.bz2 FOSSEE-Signal-Processing-Toolbox-1251f70aa3442736ce6fd9c4fb7fbce412af5a52.zip |
code changes by Kartik Hegde during FOSSEE Fellowship 2018
Diffstat (limited to 'macros/cconv.sci')
-rw-r--r-- | macros/cconv.sci | 52 |
1 files changed, 40 insertions, 12 deletions
diff --git a/macros/cconv.sci b/macros/cconv.sci index c7560ef..d3c2c2f 100644 --- a/macros/cconv.sci +++ b/macros/cconv.sci @@ -1,12 +1,40 @@ //Author: Parthasarathi Panda //parthasarathipanda314@gmail.com function o=cconv(a,b,n) + + // circularly convolves vectors a and b. n is the length of the resulting vector. + //If you omit n, it defaults to length(a)+length(b)-1. When n = length(a)+length(b)-1, + //the circular convolution is equivalent to the linear convolution computed with conv + //Calling Sequence: + //o=cconv(a,b) + //o = cconv(a,b,n) +//a =a real or complex vector. +//b =a real or complex vector. +//n =length of circular convolution +//o =convolution sequence +//Examples: +//a=[1 2 3] +//b=[4 5 6] +//o=cconv(a,b,3) +//Output: o= 31. 31. 28. +// +// +//a=[1 2+%i 4] +//b=[2 3*%i 5] +//o=cconv(a,b) +//o=clean(o) +// +//Output: o= 2. 4. + 5.i 10. + 6.i 10. + 17.i 20. +// + + + [nargout,nargin]=argn(); - if nargin==2 then - n=length(a)+length(b)-1; + if nargin==2 then //to check the number of inputs entered by the user + n=length(a)+length(b)-1;//setting the length of convolution end - if type(a)~=1 | type(b)~=1 | type(n)~=1 then - error('check the data type of input'); //to check if the inputs are real/complex arrays + if type(a)~=1 | type(b)~=1 | type(n)~=1 then//to check if the inputs are real/complex arrays + error('check the data type of input'); end if size(n)~=[1,1] then error('check the data type of input'); //to check that n is single dimensional @@ -18,27 +46,27 @@ function o=cconv(a,b,n) [i,j]=size(a); if j~=1 & i~=1 then error('a should be a vector'); - elseif j==1 + elseif j==1 //if a column vector make it a row vector a=a'; end //checking if b is a 1d vector(row or column) and turning it into row vector [i,j]=size(b); if j~=1 & i~=1 then error('b should be a vector'); - elseif j==1 + elseif j==1//if a column vector make it a row vector b=b'; end - + //adjusting length of a - if n<=length(a) then + if n<=length(a) then//if length exceeds n,then take only first n-samples a=a(1:n); - else + else//if length is less than n, then pad zeroes a=[a,zeros(1,n-length(a))] end //adjusting length of b - if n<=length(b) then + if n<=length(b) then//if length exceeds n,then take only first n-samples b=b(1:n); - else + else//if length is less than n, then pad zeroes b=[b,zeros(1,n-length(b))] end //computing ffts (for speed) @@ -46,5 +74,5 @@ function o=cconv(a,b,n) bft=fft(b); //circular convolution dft is the product of dft of the 2 oft=aft.*bft; - o=ifft(oft); + o=ifft(oft); //inverse gives circular covolution endfunction |