summaryrefslogtreecommitdiff
path: root/macros/cconv.sci
blob: c7560eff9fce17657be46164e979da95effb1cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Author: Parthasarathi Panda
//parthasarathipanda314@gmail.com
function o=cconv(a,b,n)
    [nargout,nargin]=argn();
    if nargin==2 then
        n=length(a)+length(b)-1;
    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
    end
    if size(n)~=[1,1] then
        error('check the data type of input'); //to check that n is single dimensional
    end
    if floor(n)~=n | imag(n)~=0 then
        error('check that n is an integer');//to check if n is an integer
    end
    //checking if a is a 1d vector(row or column) and turning it into row vector
    [i,j]=size(a);
    if j~=1 & i~=1 then
        error('a should be a vector');
    elseif j==1 
        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 
        b=b';
    end
    
    //adjusting length of a
    if n<=length(a) then
        a=a(1:n);
    else
        a=[a,zeros(1,n-length(a))]
    end
    //adjusting length of b
    if n<=length(b) then
        b=b(1:n);
    else
        b=[b,zeros(1,n-length(b))]
    end
    //computing ffts (for speed)
    aft=fft(a);
    bft=fft(b);
    //circular convolution dft is the product of dft of the 2
    oft=aft.*bft;
    o=ifft(oft);    
endfunction