[1476a0]: fourier / pconv.m Maximize Restore History

Download this file

pconv.m    73 lines (62 with data), 1.7 kB

 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function h=pconv(f,g,varargin)
%PCONV Periodic convolution
% Usage: h=pconv(f,g)
% h=pconv(ptype,f,g);
%
% `pconv(f,g)` computes the periodic convolution of *f* and *g*. The convolution
% is given by
%
% .. L-1
% h(l+1) = sum f(k+1) * g(l-k+1)
% k=0
%
% .. math:: h\left(l+1\right)=\sum_{k=0}^{L-1}f\left(k+1\right)g\left(l-k+1\right)
%
% `pconv('r',f,g)` computes the convolution where *g* is reversed
% (involuted) given by
%
% .. L-1
% h(l+1) = sum f(k+1) * conj(g(k-l+1))
% k=0
%
% .. math:: h\left(l+1\right)=\sum_{k=0}^{L-1}f\left(k+1\right)\overline{g\left(k-l+1\right)}
%
% This type of convolution is also known as cross-correlation.
%
% `pconv('rr',f,g)` computes the alternative where both *f* and *g* are
% reversed given by
%
% .. L-1
% h(l+1) = sum conj(f(-k+1)) * conj(g(k-l+1))
% k=0
%
% .. math:: h\left(l+1\right)=\sum_{k=0}^{L-1}f\left(-k+1\right)g\left(l-k+1\right)
%
% In the above formulas, $l-k$, $k-l$ and $-k$ are computed modulo $L$.
%
% See also: dft, involute
% AUTHOR: Peter L. S��ndergaard
% TESTING: TEST_PCONV
% REFERENCE: REF_PCONV
% Assert correct input.
if nargin<2
error('%s: Too few input parameters.',upper(mfilename));
end;
if ~all(size(f)==size(g))
error('f and g must have the same size.');
end;
definput.flags.type={'default','r','rr'};
[flags,kv]=ltfatarghelper({},definput,varargin);
if flags.do_default
h=ifft(fft(f).*fft(g));
end;
if flags.do_r
h=ifft(fft(f).*conj(fft(g)));
end;
if flags.do_rr
h=ifft(conj(fft(f)).*conj(fft(g)));
end;
% Clean output if input was real-valued
if isreal(f) && isreal(g)
h=real(h);
end;