Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS 1109 Homework 4 Solutions - Sound Effects and Chess Board Programming, Exercises of Computer Programming

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

2012/2013

Uploaded on 08/17/2013

zaid
zaid 🇮🇳

4.5

(2)

61 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 Sound Effects
1.1 Delay
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:enddn,:);
1.2 Echo
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
1.3 Fade-out
function result = fade out(data, fs, delta, damping)
% Creates a fadeout 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(enddn:end,:) = result(enddn:end,:) .*exp(damping*(0:dn)'/fs);
1
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download CS 1109 Homework 4 Solutions - Sound Effects and Chess Board Programming and more Exercises Computer Programming in PDF only on Docsity!

1 Sound Effects

1.1 Delay

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,:);

1.2 Echo

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

1.3 Fade-out

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);

2 Chess

2.1 Draw Board

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');

2.3 Display Game

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);

2.4 Animate Game

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);