Why do results of fft() on a matrix differ from fft() on one row as a vector?

조회 수: 12 (최근 30일)
WM
WM 2019년 10월 11일
댓글: WM 2019년 10월 11일
I use the built-in fft() function on a 2D single matrix of size 71*110000. Initially, I looped over the first dimension. However, passing the whole matrix to fft() results in ~30% speedup, which is why I'd like to continue with that. What's puzzling me is that the results are similar but not the same. My understanding from the documentation is that Matlab would do the exact same computation, treating each row as a vector in the matrix case. I'd be thankful for any idea why the results differ.
Example:
%% generate random data
data = single(rand(71, 110000));
n = 111872;
%% loop version
for ichan = 1:size(data, 1)
dataX(ichan, :) = fft(data(ichan, :), n);
end
%% matrix version
alldataX = fft(data, n, 2);
%% test
isequal(dataX, alldataX)
isequaln(dataX, alldataX)
max(abs(dataX - alldataX)) %small difference, but not the same
  댓글 수: 3
Bjorn Gustavsson
Bjorn Gustavsson 2019년 10월 11일
This looks like floating-point rounding error effects. It is a bit troubling that the results differ. The reason might be that some of the calculations are done differently in the fftw-code. At first I thought it might have to do with the ordering of the prime-factors of your 111872, but the problem persists even with data with a size that's an even power of 2. I suggest that you contact mathworks directly about this - and please let us know about their response.
WM
WM 2019년 10월 11일
@dbp, you're right - of course the maximum absolute difference is a more meaningful measure. I edited the original question accordingly. Differences still exist, though. As all of you suggested floating point rounding differences, and Matt J explained the reason for it, i'll accept his answer. Thanks for the quick replies!

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2019년 10월 11일
편집: Matt J 2019년 10월 11일
The same dicrepancy exists with most Matlab functions that do operations along rows or columns. Here is the same test replacing fft with sum,
%% generate random data
data = single(rand(71, 110000));
dataX=data(:,1);
%% loop version
for ichan = 1:size(data, 1)
dataX(ichan) = sum(data(ichan, :), 2);
end
%% matrix version
alldataX = sum(data, 2);
%% test
isequal(dataX, alldataX)
isequaln(dataX, alldataX)
max(abs(dataX(:) - alldataX(:)))/max(alldataX(:))*100 %4.2499e-05
The reason for it is that Matlab's internal multi-threading splits the data up differently depending on the size of the array given as input to the function. This leads to summations being done in different orders and hence floating point discrepancies in the results.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by