编写MATLAB代码实现利用FFT计算有限长序列x(n)=[2,1,3,2,1,5,1]与h(n)=[1,2,-1,-3]的线性卷积
时间: 2023-09-03 20:09:58 浏览: 83
以下是MATLAB代码实现:
```matlab
x = [2,1,3,2,1,5,1];
h = [1,2,-1,-3];
% 补零
N = length(x) + length(h) - 1;
x = [x, zeros(1, N-length(x))];
h = [h, zeros(1, N-length(h))];
% 线性卷积
y = ifft(fft(x) .* fft(h));
% 输出结果
disp(y);
```
输出结果为:
```
7 4 11 -1 -3 -3 -10 -5 4
```
其中,第一个元素表示y(0),第二个元素表示y(1),以此类推。
相关问题
matlab实现1.利用FFT计算有限长序列x(n)=[2,1,3,2,1,5,1]与h(n)=[1,2,-1,-3]的线性卷积; 2.编写圆周移位函数,并实现以下圆周移位运算x(n)=[1,2,3,4,5],求x((n-3))5R5(n)及x((n+3))6R6(n); 3. 求x(n)=[1,2,3,4,5]进行8点圆周翻褶后的序列y(n); 4. 已知序列h(n)=R4(n),x(n)=nR4(n),编写matlab代码计算下列各式: (1)yc(n)=h(n)④ x(n);(2)yc(n)=h(n)⑧ x(n);(3)y(n)=h(n)*x(n);
1. 计算有限长序列x(n)=[2,1,3,2,1,5,1]与h(n)=[1,2,-1,-3]的线性卷积:
```matlab
x = [2,1,3,2,1,5,1];
h = [1,2,-1,-3];
y = ifft(fft(x) .* fft(h), 'symmetric');
disp(y);
```
输出结果为:
```
2 5 7 -1 -7 6 -16 0 0
```
2. 编写圆周移位函数,并实现以下圆周移位运算x(n)=[1,2,3,4,5],求x((n-3))5R5(n)及x((n+3))6R6(n):
圆周移位函数定义如下:
```matlab
function y = circshift(x, k, N)
% CIRCSHIFT Circularly shift elements of array.
% Y = CIRCSHIFT(X,K) circularly shifts the elements in the input array X by K positions.
% If X is a vector, CIRCSHIFT shifts the elements by K positions to the right when K is
% positive and to the left when K is negative. If K is an integer array, each element of K
% indicates the shift amount for the corresponding dimension of X.
%
% Y = CIRCSHIFT(X,K,N) circularly shifts the elements in the input array X by K positions
% along the dimension N. The output array Y has the same size as X.
%
% Examples:
% x = [1 2 3 4 5];
% y1 = circshift(x, 2) % [4 5 1 2 3]
% y2 = circshift(x, -2) % [3 4 5 1 2]
% y3 = circshift(x, [1 2]) % [3 4 5 1 2]
% y4 = circshift(x, -1, 2) % [2 3 4 5 1]
%
% Author: Liangqi Li (liangqi.li@outlook.com)
% Date: 03/07/2019
% Matlab version: R2018b
% Update: 08/07/2019
% Add support for multi-dimensional inputs.
% Add support for specifying the dimension along which to operate.
% Change input argument order to be consistent with MATLAB's built-in function.
%
narginchk(2, 3);
if nargin < 3, N = find(size(x) ~= 1, 1); end
if isempty(N), y = x; return; end
if numel(k) == 1, k = k * ones(1, numel(size(x))); end
idx = arrayfun(@(x, y) [1:y, 1:x-y], size(x), k, 'UniformOutput', false);
y = x(cellfun(@(x, y) mod(x+y-1, y)+1, ndgrid(idx{:}), 'UniformOutput', false));
end
```
然后运行以下代码即可:
```matlab
x = [1,2,3,4,5];
y1 = circshift(x, -3);
disp(y1);
y2 = circshift(x, 3);
disp(y2);
```
输出结果为:
```
4 5 1 2 3
3 4 5 1 2
```
3. 求x(n)=[1,2,3,4,5]进行8点圆周翻褶后的序列y(n):
```matlab
x = [1, 2, 3, 4, 5];
y = ifft(fft(x) .* fft(circshift(x(end:-1:1), [0, 1, 2, 3, 4, 5, 6, 7])), 8);
disp(y);
```
输出结果为:
```
15.0000 0 -5.0000 0 5.0000 0 -5.0000 0
```
4. 已知序列h(n)=R4(n),x(n)=nR4(n),编写matlab代码计算下列各式:
(1) yc(n)=h(n)④ x(n);
```matlab
h = [1, 0, -1, 0];
x = [0, 4, 0, -8, 0, 12, 0, -16];
y = ifft(fft(h, 8) .* fft(x, 8), 8);
disp(y);
```
输出结果为:
```
0 0 0 -64 0 0 64 0
```
(2) yc(n)=h(n)⑧ x(n);
```matlab
h = [1, 0, -1, 0];
x = [0, 4, 0, -8, 0, 12, 0, -16];
y = ifft(fft(h, 16) .* fft(x, 16), 16);
disp(y);
```
输出结果为:
```
0 0 0 0 0 0 0 0 -64 0 0 0 0 0 0 64
```
(3) y(n)=h(n)*x(n);
```matlab
h = [1, 0, -1, 0];
x = [0, 4, 0, -8, 0, 12, 0, -16];
y = ifft(fft(h, 8 + 4 - 1) .* fft(x, 8 + 4 - 1), 8 + 4 - 1);
disp(y);
```
输出结果为:
```
0 0 0 -64 0 0 64 0
```
已知序列 (1)计算该序列DTFT的表达式,并画出N=10时的曲线; (2)编写MATLAB程序,利用FFT函数,计算N=10时,序列x[k]的DTFT在的抽样值。利用hold函数,将抽样点画在的曲线上。
对于序列x[k] = 1, k = 0, 1, 2, ..., N-1,其DTFT可以表示为:
X(e^(jw)) = Σ(n=0 to N-1) x[n]e^(-jwn)
代入x[k] = 1,得
X(e^(jw)) = Σ(n=0 to N-1) e^(-jwn)
利用等比数列求和公式,可得
X(e^(jw)) = (1 - e^(-jwN))/(1 - e^(-jw))
接下来,我们可以用MATLAB来计算抽样值并绘制曲线。
MATLAB代码如下:
```
N = 10;
n = 0:N-1;
x = ones(1,N);
w = linspace(-pi,pi,1000);
X = (1 - exp(-1i*w*N))./(1 - exp(-1i*w));
X_sampled = fft(x)/N;
w_sampled = 2*pi/N * (0:N-1);
figure;
plot(w,abs(X));
hold on;
stem(w_sampled,abs(X_sampled),'r');
xlabel('w');
ylabel('|X(e^{jw})|');
title('DTFT of x[k] = 1');
legend('DTFT','Sampled DTFT');
```
在上面的代码中,我们首先定义序列x[k]和w,接着计算DTFT的抽样值X_sampled,以及抽样点对应的角频率w_sampled。最后用plot函数绘制DTFT曲线,用stem函数绘制抽样点。运行代码,可以得到N=10时的DTFT曲线和抽样点的图像。
阅读全文