一、自定义卷积
function y = Convu(x, W)
Nx = length(x);
Nw = length(W);
y = zeros(1, Nx + Nw - 1);
for n = 1:Nx + Nw - 1
sum = 0;
startIdx = max(1, n - Nw + 1);
endIdx = min(n, Nx);
for k = 1:Nw
if startIdx + k - 1 <= endIdx
sum = sum + x(startIdx + k - 1) * W(Nw - k + 1);
end
end
y(n) = sum;
end
end
二、计算窗函数的卷积
N = 100;
W = ones(1, N);
figure
stem(W);
figure
Fs = 201;
T = 1/Fs;
L = 100;
t = (0:L-1)*T;
f = 2;
A = 1;
x = A*sin(2*pi*f*t );
stem(t,x);
y = Convu(x, W);
t_conv = (0:length(y)-1) * T;
figure;
stem(t_conv, y, 'filled');
title('卷积');
grid on;