Docsity

CS 1371 Fall 2006 Practice Test 3 Solutions, Exams of Computer Science

Toggle sidebar
bg1
CS 1371 โ€“ Fall 2006
Practice Test 3 Solutions
Sounds
Problem 1
function test3sound(filename,n)
[Y Fs] = wavread(filename);
L = length(Y);
X = round(linspace(1,L,round(L/(2^(n/12)))));
sound(Y(X), Fs)
% Alternatively the next line could be used in place of
% lines 4-6
% sound(Y, round(Fs * 2^(n/12)))
Problem 2
function sounds2(filename,n)
[Y Fs] = wavread(filename);
L = length(Y);
X = round(linspace( 1, L, round(L*n)));
Ynew = Y(X);
wavwrite(Ynew, Fs, ['new' filename]);
Problem 3
[Y Fs] = wavread('music.wav');
L = length(Y);
T = L / Fs; % Total duration
Fs = 44100;
t = 1/Fs:1/Fs:1;
% Duration here is just assumed 1 even though not stated
% in problem
Y = cos(2*pi*(261.6).*t);
sound(Y,Fs);
pf3
pf4
pf5

Partial preview of the text

Download CS 1371 Fall 2006 Practice Test 3 Solutions and more Exams Computer Science in PDF only on Docsity!

CS 1371 โ€“ Fall 2006

Practice Test 3 Solutions

Sounds

Problem 1

function test3sound(filename,n)

[Y Fs] = wavread(filename); L = length(Y); X = round(linspace(1,L,round(L/(2^(n/12))))); sound(Y(X), Fs)

% Alternatively the next line could be used in place of % lines 4- % sound(Y, round(Fs * 2^(n/12)))

Problem 2

function sounds2(filename,n) [Y Fs] = wavread(filename); L = length(Y); X = round(linspace( 1, L, round(L*n))); Ynew = Y(X); wavwrite(Ynew, Fs, ['new' filename]);

Problem 3

[Y Fs] = wavread('music.wav'); L = length(Y); T = L / Fs; % Total duration

Fs = 44100; t = 1/Fs:1/Fs:1; % Duration here is just assumed 1 even though not stated % in problem Y = cos(2pi(261.6).*t); sound(Y,Fs);

Images

Problem 1

function ret = GreenUpsideDown(img) ret = img(end:-1:1, :,:); % flip ret = double(ret); ret(:,:,2) = (ret(:,:,1)+ret(:,:,3))./2; ret = uint8(ret);

Problem 2

function ret = grayscale(filename) img = imread(filename); img = double(img); ret = (img(:,:,1) + img(:,:,2) + img(:,:,3))./3; ret = uint8(ret); imshow(ret);

Numerical Methods

Problem 1

function test3nummeth(x,y) newx = linspace(x(1), x(end), 50); newy = spline(x,y,newx); plot(x,y,newx,newy); title('cubic spline interpolation plot'); xlabel('X values'); ylabel('Y values');

Problem 2

coef = polyfit(data(1,:), data(2,:), 3) x = linspace(data(1,1), data(1,end), 10 * length(data(1,:))); y = polyval(coef,x); plot(x,y);

Problem 3

A = interp1(t,c,[40], 'linear') coef = polyfit(t,c,2); B = polyval(coef,[40]) plot(t(2:end),diff(c)./diff(t));

Sorting

Problem 1

Quick Sort โ€“ N log N

Bubble Sort โ€“ N^

Insertion Sort โ€“ N^

Merge Sort โ€“ N log N

Problem 2

quick sort

[36 25 89 41 20 69 6 99]

[25 20 6] [36] [89 41 69 99]

[20 6] [25] [36] [41 69] [89] [99]

[6] [20] [25] [36] [41] [69] [89] [99]

[6 20 25 36 41 69 89 99]

bubble sort

[36 25 89 41 20 69 6 99]

[25 36 41 20 69 6 89 99]

[25 36 20 41 6 69 89 99]

[25 20 36 6 41 69 89 99]

[20 25 6 36 41 69 89 99]

[20 6 25 36 41 69 89 99]

[6 20 25 36 41 69 89 99]

insertion sort

[36 25 89 41 20 69 6 99]

[36]

[25 36]

[25 36 89]

[25 36 41 89]

[20 25 36 41 89]

[20 25 36 41 69 89]

[6 20 25 36 41 69 89]

[6 20 25 36 41 69 89 99]

merge sort

[36 25 89 41 20 69 6 99]

[36 25 89 41] [20 69 6 99]

[36 25] [89 41] [20 69] [6 99]

[36] [25] [89] [41] [20] [69] [6] [99]

[25 36] [41 89] [20 69] [6 99]

[25 36 41 89] [6 20 69 99]

[6 20 25 36 41 69 89 99]