![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/535521/image.png)
Help on zero padding and understanding of the following example?
1 次查看(过去 30 天)
显示 更早的评论
To understand the concept and impact of zero padding on computation time, I have created an example below and run the program and note the time taken to complete the program. Using the following program I assume, I will get a lowest time using 3 (with zero padding to array1, see below), but this was not the case. I got the different result than my assumption! Where did I make the mistake? Could you help me?
% Create an example of zero-padding
close all; clear; clc; workspace
array1 = rand(41,41,41);
array2 = ones(256,256,199);
%% 1. direct convolution
tic
A = convn(array1, array2);
toc
%% 2. without zero padding: FFT method
tic
B = fftn(array1);
C = fftn(array2);
D = ifftn(convn(B, C));
toc
%% 3. with zero padding to array1
zero_paded_array1 = zeros(256,256,199);
% insert array1 in the middle of zeros matrix
zero_paded_array1(108:148, 108:148, 79:119)= array1;
tic
E = fftn(zero_paded_array1);
F = fftn(array2);
% Now, E and F are of same size
G = ifftn(convn(E, F));
toc
Elapsed time is 36.725312 seconds.
Elapsed time is 7.637434 seconds.
Elapsed time is 197.981833 seconds. ....>>>> If the zero padding was right then this time should be lower than above two, right?
0 个评论
回答(1 个)
KALYAN ACHARJYA
2021-3-1
编辑:KALYAN ACHARJYA
2021-3-1
"If the zero padding was right then this time should be lower than above two, right?"
How?
The only difference between section 2 and 3 are B and E, remain code are exactly same (Within tic toc)
>> whos B
Name Size Bytes Class Attributes
B 41x41x41 1102736 double complex
>> whos E
Name Size Bytes Class Attributes
E 256x256x199 208666624 double complex
E here
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/535521/image.png)
Both section (2 and 3) are performing the same operation, except for one variable in segnment 3, which has larger data size (E) as compare to it's counterpart (B) in section 2, therefore expecting higher execution time than section 2
Hello experts and others members, Am I missing something here? Or this is a simple case, as I have mentioned.
2 个评论
Allen
2021-3-1
Not sure what the output from these functions is supposed to look like in terms of array size, but the results from method 2 and 3 both produce a resulting array whos size is the sum of the size of each input array minus one. Since the size of E>B, this also means that size of G>D and hence the longer computational time.
>> size(B)+size(C)-1
ans =
296 296 239
>> size(D)
ans =
296 296 239
>> size(E)+size(F)-1
ans =
511 511 397
>> size(G)
ans =
511 511 397
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!