



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to homework 4 of cs 1109, including functions for creating sound effects like delay, echo, and fade-out, as well as a chess board program with functions for drawing the board, showing pieces, displaying the game, and animating the game.
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




function result = delay(data, fs, delta) % Delays the sound stored in data by an amount delta given in seconds. % Each data point is seperated by 1/fs seconds.
dn = floor(delta (^) * fs); result = zeros(size(data)); result(dn+1:end,:) = data(1:end−dn,:);
function result = echo effect(data, fs, num echos, delta, damping) % Creates one or multiple echoes seperated by a time difference % delta which is given in seconds. Each echo has a lower sound % level than its source. The ratio of the levels is given by the % variable damping.
result = data; for n = 1:num echos delayed = (dampingˆn) (^) * delay(data, fs, n*delta); result = result + delayed; end
function result = fade out(data, fs, delta, damping) % Creates a fade−out effect after delta seconds. The damping parameter is % used in the multiplier function, e.g. exp(−damping (^) * someOtherStuff).
result = data; dn = length(data) − floor(delta (^) * fs); result(end−dn:end,:) = result(end−dn:end,:) .* exp(−damping*(0:dn)'/fs);
function imboard = draw board(filename, side) % Returns an image data for a chessboard. Stores the result in a file. % The side length of a square is given in pixels.
% There are various way you can start with an empty board. % Here we 'paint' a gray color everywhere, and later we will % update the color of white squares. The reason is that black % pieces provided don't have a border and when they are shown % on black squares, they become invisible. % Also notice that we can directly create 'ones' of 'uint8' type. imboard = 100ones(side8,side*8,3,'uint8');
for r = 1: for c = 1: if rem(r + c,2) == 0 imboard(side(r−1)+1:sider,side(c−1)+1:sidec,:) = 255; end end end
imwrite(imboard, filename,'png');
function display game(filename, delay) % Reads moves in coordinate notation from a textfile. Shows them on the % chessboard. Waits delay seconds in between the moves.
f = fopen(filename,'r');
board = initialize(); show pieces(board);
moves = fscanf(f, '%c%c%c%c%c\n', [5 inf])' for i = 1:size(moves,1) pause(delay); board = move piece(board, moves(i,:)); show pieces(board); end
fclose(f);
function animate game(movie fname, game fname, delay) % Creates an animation of a chess game and stores it as a movie
f = fopen(game fname,'r');
board = initialize(); show pieces(board);
moves = fscanf(f, '%c%c%c%c%c\n', [5 inf])'
M(1) = getframe; for i = 1:size(moves,1) pause(delay); board = move piece(board, moves(i,:)); show pieces(board); M(i+1) = getframe; end fclose(f);
movie2avi(M,movie fname,'FPS',1/delay);