Homework 4: Due Midnight on Tuesday October 27th.

Functions

Write a function called 'insertMatrix' that inserts copies of one matrix inside another larger matrix.

The function should take in the following arguments (in order):

1) a large matrix (or vector)

2) a smaller matrix (or vector)

3) a list of row values (as a vector) that determine the top location of the inserted matrix

4) a list of column values (as a vector the same size as the rows) that determine the left location of the inserted matrix.

By default (if not entered as arguments), the rows and column values should be set to 1

Note: the larger and smaller matrices shouldn't have to be square. We've given four examples below of how you should be able to use this function. If you made your function correctly, it should produce the images and sound described below.


Example 1: A simple use of the function

mat1 = ones(100); %background (large) matrix
mat2 = 2*ones(10,10); %square

row = [30,30]; %row positions for two inserted squares
col = [25,65]; %corresponding column positions

%insert two copies of mat2 into mat1
mat1 = insertMatrix(mat1,mat2,row,col);

mat3 = 2*ones(10,60); %rectangle
row = 70; %row position
col = 20; %column position
%insert one copy of mat3 into mat1
mat1 = insertMatrix(mat1,mat3,row,col);

%show mat1 as b/w image
figure(1)
clf
image(mat1);
colormap(gray(2))
axis equal
axis off


Example 2: Placing some gratings inside a noise image.

noiseSize = [300,400]; %size of larger noise image (columns, rows)
gratingSize = 50; %size of smaller grating image (assume square)

%start with a noise image
img = 2*rand(noiseSize)-1;

%make the grating image
[x,y] = meshgrid(linspace(-1,1,gratingSize));

sf =2; %number of cycles for the grating
grating = cos(sf*2*pi*x);

%List of rows and columns for inserting:

row = 20:50:220; %three row values
col = 10:80:330; % corresponding three column values

%This is the call to our new function
img= insertMatrix(img,grating,row,col);

figure(2)
clf
image((img+1)*127.5)
colormap(gray(256))
axis equal
axis off


Example 3: random dot stereograms

A random dot stereogram is a pair of black and white noise images designed to be viewed with the left and right eyes separately to see an impression of depth through stereopsis. In this example, the right image the same as the left except it has a square copied and shifted to the right. When 'free fused' (viewed with crossed eyes so that you only see one central image), you'll see a center square hovering in front of the background.

backgroundSize = 100; %Background image size
squareSize = 50;
leftImg = floor(rand(backgroundSize)+.5);

row = 25;
col = 25;
shift = 5; %pixels

squareImg = leftImg(row:(row+squareSize-1),col:(col+squareSize-1)); %'copy' the central square
rightImg = insertMatrix(leftImg,squareImg,row,col+shift); %'paste' the square, shifted rightward

figure(3)
clf

subplot(1,2,1)
image(leftImg*256);
colormap(gray(256));
axis equal
axis off

subplot(1,2,2)
image(rightImg*256);
colormap(gray(256));
axis equal
axis off

 



Example 4: Sound

Since our large and small matrices can be any size, they can therefore be sound vectors. Next we'll use our function to insert some brief white noise sounds in the middle of a pure tone.

toneDur = 3; %Duration of tone (sec)
noiseDur = .25; %Duration of noise (sec)
sampRate = 14400; %Hz
freq = 1000; %Hz
tStart = .5:1:2.5; %times of start of noise sound (sec)

tTone = linspace(0,toneDur,toneDur*sampRate);
tNoise = linspace(0,noiseDur,noiseDur*sampRate);

tone = sin(2*pi*tTone*freq);
noise = .25*randn(size(tNoise));

col = ceil(tStart*sampRate);

newSound = insertMatrix(tone,noise,ones(size(col)),col);
sound(newSound,sampRate)


 

Example 5: One more example.

Some students used a double-loop through 'row' and 'col' instead of a single loop through the length of 'row' (or 'col'). Here's an example that should reveal this bug.

mat1 = ones(5,12);
mat2 = 2*ones(1,2);

row = [2,4];
col = [2,10];

mat = insertMatrix(mat1,mat2,row,col);

figure(1)
image(mat);
colormap(gray(2));
axis equal
axis off

The result should look like this:

Instead of this: