《DSP using MATLAB》Problem 5.24-5.25-5.26

Posted ky027wh-sx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《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

  主程序:

%% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%%            Output Info about this m-file
fprintf(‘
***********************************************************
‘);
fprintf(‘        <DSP using MATLAB> Problem 5.24 

‘);

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

  

%% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%%            Output Info about this m-file
fprintf(‘
***********************************************************
‘);
fprintf(‘        <DSP using MATLAB> Problem 5.25 

‘);

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

  

%% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%%            Output Info about this m-file
fprintf(‘
***********************************************************
‘);
fprintf(‘        <DSP using MATLAB> Problem 5.26 

‘);

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的主要内容,如果未能解决你的问题,请参考以下文章

《DSP using MATLAB》Problem 5.7

《DSP using MATLAB》Problem 6.7

《DSP using MATLAB》Problem 6.8

《DSP using MATLAB》Problem 3.12

《DSP using MATLAB》Problem 4.17

《DSP using MATLAB》Problem 3.5