【发布时间】:2015-10-22 12:54:57
【问题描述】:
我在 Matlab 中为 4D 图像(4D 矩阵)实现了带通滤波器。前三个维度是空间维度,最后一个维度是时间维度。代码如下:
function bandpass_img = bandpass_filter(img)
% Does bandpass filtering on input image
%
% Input:
% img: 4D image
%
% Output:
% bandpass_img: Bandpass filtered image
TR = 1; % Repetition time
n_vols = size(img,3);
X = [];
% Create matrix (voxels x time points)
for z = 1:size(img,3)
for y = 1:size(img,2)
for x = 1:size(img,1)
X = [X; squeeze(img(x,y,z,:))']; %#ok<AGROW>
end
end
end
Fs = 1/TR;
nyquist = 0.5*Fs;
% Pass bands
F = [0.01/nyquist, 0.1/nyquist];
type = 'bandpass';
% Filter order
n = floor(n_vols/3.5);
% Ensure filter order is odd for bandpass
if (mod(n,2) ~= 0), n=n+1; end
fltr = fir1(n, F, type);
% Looking at frequency response
% freqz(fltr)
% Store plot to file
% set(gcf, 'Color', 'w');
% export_fig('freq_response', '-png', '-r100');
% Apply to image
X = filter(fltr, 1, X);
% Reconstructing image
i = 1;
bandpass_img = zeros(size(img));
for z = 1:size(img,3)
for y = 1:size(img,2)
for x = 1:size(img,1)
bandpass_img(x,y,z,:) = X(i,:)';
i = i + 1;
end
end
end
end
我不确定实施是否正确。有人可以验证它还是有人发现失败?
编辑:感谢 SleuthEye,当我使用 bandpass_img = filter(fltr, 1, img, [], 4); 时,它现在可以正常工作了。但是还有一个小问题。我的图像尺寸为 80x35x12x350,即有 350 个时间点。我已经绘制了应用带通滤波器前后的平均时间序列。
带通滤波前:
带通滤波后:
为什么这个峰值出现在过滤图像的最开始?
编辑 2:现在在开始和结束处都有一个峰值。见:
我制作了第二个图,其中我用 * 标记了每个点。见:
所以第一个和最后两个时间点似乎更低。
看来我要去掉开头的2个时间点,最后还要去掉2个时间点,所以一共要去掉4个时间点。
你怎么看?
【问题讨论】:
标签: matlab filtering signal-processing lowpass-filter highpass-filter