# 《DSP using MATLAB》Problem 5.24-5.25-5.26

```function y = circonvt(x1,x2,N)
%% N-point Circular convolution between x1 and x2: (time domain)
%% ------------------------------------------------------------------
%% [y] = circonvt(x1,x2,N)
%% y  = output sequence containning the circular convolution
%% x1 = input sequence of length N1 <= N
%% x2 = input sequence of length N2 <= N
%%
%% N = size of circular buffer
%% Method: y(n) = sum( x1(m)*x2((n-m) mod N) )
%% Check for length of x1

if length(x1) > N
error(‘N must be >= the length of x1 !‘)
end
%% Check for length of x2

if length(x2) > N
error(‘N must be >= the length of x2 !‘)
end

x1 = [x1 zeros(1,N-length(x1))];
x2 = [x2 zeros(1,N-length(x2))];

m = [0:1:N-1]; x2 = x2(mod_1(-m, N)+1); H = zeros(N,N);
for n = 1:1:N
H(n,:) = cirshftt(x2,n-1,N);
end
y = x1*conj(H‘);         % x1---row vector
%    H
%    y = H*x1‘;               % x1---column vector
```

主程序：

```%% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fprintf(‘\n***********************************************************\n‘);
fprintf(‘        <DSP using MATLAB> Problem 5.24 \n\n‘);

banner();
%% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

% -------------------------------------------------------------------
%
% -------------------------------------------------------------------
N = 4;
n1 = [0:3];
x1 = [1, 2, 2];

x2 = [1, 2, 3, 4];

y1 = circonvt(x1, x2, N)
```

运行结果：

```function [C] = circulnt(x, N)
%%  Circulant Matrix from an N-point sequence
%% ------------------------------------------------------------------
%% [C] = circulnt(x, N)
%%  C  = Circulant Matrix of size NxN
%%  x  = sequence of length <= N
%%
%%  N  = size of circulant matrix
if length(x) > N
error(‘N must be >= the length of x !‘)
end

x = [x zeros(1, N-length(x))];

for i = 1 : N
c(i) = x(i);
end

m = [0:1:N-1]; x_fold = x(mod_1(-m, N)+1);
r = x_fold;

C = toeplitz(c,r);
```

```function y = circonvt_v3(x1,x2,N)
%% N-point Circular convolution between x1 and x2: (time domain)
%% ------------------------------------------------------------------
%% [y] = circonvt(x1,x2,N)
%% y  = output sequence containning the circular convolution
%% x1 = input sequence of length N1 <= N
%% x2 = input sequence of length N2 <= N
%%
%% N = size of circular buffer
%% Method: y(n) = sum( x1(m)*x2((n-m) mod N) )
%% Check for length of x1

if length(x1) > N
error(‘N must be >= the length of x1 !‘)
end
%% Check for length of x2
if length(x2) > N
error(‘N must be >= the length of x2 !‘)
end

x1 = [x1 zeros(1,N-length(x1))];
x2 = [x2 zeros(1,N-length(x2))];

C = circulnt(x2, N);

%	m = [0:1:N-1]; x2 = x2(mod_1(-m, N)+1); H = zeros(N,N);
%	for n = 1:1:N
%		H(n,:) = cirshftt(x2,n-1,N);
%	end
%	y = x1*conj(H‘);         % x1---row vector

y = C*x1‘;               % x1---column vector
```

```%% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fprintf(‘\n***********************************************************\n‘);
fprintf(‘        <DSP using MATLAB> Problem 5.25 \n\n‘);

banner();
%% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

% -------------------------------------------------------------------
%
% -------------------------------------------------------------------
N = 4;
n1 = [0:3];
x1 = [1, 2, 2];

x2 = [1, 2, 3, 4];

%C = circulnt(x2, 4);

y1 = circonvt_v3(x1, x2, N)
```

运行结果：

```function x3 = circonvf(x1, x2, N)
%% N-point Circular convolution between x1 and x2: (frequency domain)
%% ------------------------------------------------------------------
%% [x3] = circonvf(x1,x2,N)
%% x3 = output sequence containning the circular convolution
%% x1 = input sequence of length N1 <= N
%% x2 = input sequence of length N2 <= N
%%
%% N = size of circular buffer
%% Method: x3(n) = IDFT[X1(k)X2(k)]

%% Check for length of x1
if length(x1) > N
error(‘N must be >= the length of x1 !‘)
end
%% Check for length of x2
if length(x2) > N
error(‘N must be >= the length of x2 !‘)
end

x1 = [x1 zeros(1,N-length(x1))];
x2 = [x2 zeros(1,N-length(x2))];

X1k_DFT = dft(x1, N);
X2k_DFT = dft(x2, N);

x3 = real(idft( X1k_DFT.* X2k_DFT, N));
```

```%% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fprintf(‘\n***********************************************************\n‘);
fprintf(‘        <DSP using MATLAB> Problem 5.26 \n\n‘);

banner();
%% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

% -------------------------------------------------------------------
%
% -------------------------------------------------------------------
N = 4;
n1 = [0:3];
x1 = [4,3,2,1];
%x1 = [1,2,2];

n2 = [0:3];
x2 = [1, 2, 3, 4];

%C = circulnt(x2, 4);

y1 = circonvf(x1, x2, N)
```

运行结果：

《DSP using MATLAB》Problem 5.24-5.25-5.26

(0)
(0)

0条