重叠相加法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/92e74674d12f4ec7c2d39e9d98e6226c.png)
%--------------------------------------------------------------------------
% 清空一切
clc;clear all;close all;
%--------------------------------------------------------------------------
% 设置滤波器系数及数据
h = [-141,-292,23,1019,2003,2003,1019,23,-292,-141]; % 滤波器系数
x = linspace(1,30,30);
%--------------------------------------------------------------------------
% 直接计算卷积
result_A = conv(h,x);
big_len = length(result_A);
%--------------------------------------------------------------------------
% 重叠相加法
N = length(h);
M = 11;
small_len = N + M - 1;
%对x进行分段处理
x_pad = [x zeros(1,ceil(length(x)/M)*M-length(x))];
x_multi = [reshape(x_pad,M,length(x_pad)/M)'];
h_multi = repmat(h ,length(x_pad)/M,1);
%分段计算卷积
for ii=1:length(x_pad)/M
result_temp(ii,:) = conv(x_multi(ii,:),h_multi(ii,:));
end
%处理分段结果
result_temp = [result_temp zeros(length(x_pad)/M,big_len-small_len)];
for ii=1:length(x_pad)/M
end
result_temp_new(1,:) = circshift(result_temp(1,:),0);
result_temp_new(2,:) = circshift(result_temp(2,:),M);
result_temp_new(3,:) = circshift(result_temp(3,:),2*M);
result_B = sum(result_temp_new);
%比较分析
result_diff = result_B - result_A;
figure;
plot(result_A,'r');
plot(result_B,'b');
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fe24e83f651498a4ae6f1d78e4c8248c.png)
重叠保留法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/911db71a15f548cae241bd00037275ef.png)
%--------------------------------------------------------------------------
% 清空一切
clc;clear all;close all;
%--------------------------------------------------------------------------
% 设置滤波器系数及数据
h = [-141,-292,23,1019,2003,2003,1019,23,-292,-141]; % 滤波器系数
x = linspace(1,30,30);
%--------------------------------------------------------------------------
% 直接计算卷积
result_A = conv(h,x);
big_len = length(result_A);
%--------------------------------------------------------------------------
% 重叠保留法
N = length(h);
M = 11;
small_len = N + M - 1;
%对x进行分段处理
x_pad = [zeros(1,N-1) x];
op_num = ceil((length(x_pad)-M)/(small_len-M));
x_pad = [x_pad zeros(1,op_num*small_len-length(x_pad))];
x_multi = zeros(op_num,small_len);
offset = 0;
for ii=1:op_num
start_index = (ii-1)*M+1;
end_index = ii*M+N-1;
x_multi(ii,:) = x_pad(1,start_index:end_index);
end
h_multi = repmat(h ,op_num,1);
%分段计算卷积
for ii=1:op_num
result_temp(ii,:) = conv(x_multi(ii,:),h_multi(ii,:));
end
%处理分段结果
result_temp_new = result_temp(:,N:N+M-1);
result_temp_new = reshape(result_temp_new',1,size(result_temp_new,1)*size(result_temp_new,2));
result_B = result_temp_new(1,1:big_len);
%比较分析
result_diff = result_B - result_A;
figure;
plot(result_A,'r');
plot(result_B,'b');
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4e3dfe815e9f19be13a2e7560b707c9c.png)