求高手编写一个MATLAB里面的自定义函数,具有fftshift的功能。多谢了!!!

2024-11-22 08:41:49
推荐回答(1个)
回答1:

function y = fftshift(x,dim)
if nargin > 1
if (~isscalar(dim)) || floor(dim) ~= dim || dim < 1
error(message('MATLAB:fftshift:DimNotPosInt'))
end
idx = repmat({':'}, 1, max(ndims(x),dim));
m = size(x, dim);
p = ceil(m/2);
idx{dim} = [p+1:m 1:p];
else
numDims = ndims(x);
idx = cell(1, numDims);
for k = 1:numDims
m = size(x, k);
p = ceil(m/2);
idx{k} = [p+1:m 1:p];
end
end
y = x(idx{:});