Sunday, May 26, 2019

Mirror image data with MATLAB

A sequence is even if x[midpoint-n] = x[midpoint+n] for some midpoint and for all n. There are many cases where it is handy to have a even sequence. For Instance, the Fourier transform of an even sequence is real and it is handy to have a real function in the transform domain.

Below is the some MATLAB code that will make a sequence even by appending a mirror image section. The function will mirror image one, two and three dimensional data.


function DataMirrorImage = MirrorImageData(Data)
% This function mirror images one, two and three dimensional data in all
% in one, two or three dimensions.
%
% INPUT: 
% Data3D................The data to be mirror imaged.
% 
% OUTPUT:
% DataMirrorImage.....The mirror image data.

% Get the dimensions of the data.
[xLen, yLen, zLen] = size(Data);

% Mirror image the data in 1D or 2D.
for zNdx = 1:zLen
    Data2D = squeeze(Data(:,:,zNdx));
    Data2D = [ Data2D fliplr(Data2D(:,2:yLen))];
    Data2DMirrorImage = [ Data2D; flipud(Data2D(2:xLen,:))];
    DataMirrorImage(:,:,zNdx) = Data2DMirrorImage;
end

% If data is 3D , mirror image the data in 3D.
if zLen > 1
    Data3DMirrorImage(:,:,1:zLen) = DataMirrorImage;
    Data3DMirrorImage(:,:,(zLen+1):(2*zLen-1)) = flipdim(DataMirrorImage(:,:,2:zLen),3);
    DataMirrorImage = Data3DMirrorImage;
end


Example:

>> x = randn(1,9)
x =
    1.4172    0.6715   -1.2075    0.7172    1.6302    0.4889    1.0347    0.7269   -0.3034
>> xMi = MirrorImageData(x)
xMi =
  Columns 1 through 10
    1.4172    0.6715   -1.2075    0.7172    1.6302    0.4889    1.0347    0.7269   -0.3034   -0.3034
  Columns 11 through 17
    0.7269    1.0347    0.4889    1.6302    0.7172   -1.2075    0.6715

>> x = randn(9);
>> imagesc(x)


>> imagesc(MirrorImageData(x))






No comments:

Post a Comment

November Fog