Revision as of 08:36, 19 January 2011 by James4 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

ECE 301 Homework #1: Playing Sounds in Matlab


Matlab Program:

%--------------------------------------------------------------------------
% ECE 301 Homework #1
% Playing Sounds in MATLAB
%
% Michael James
% January 19th, 2011
%--------------------------------------------------------------------------

clear %This erases all previous variables in MatLab

%--------------------------------------------------------------------------
% Part 1: Three renditions of "Smoke on the Water"
%--------------------------------------------------------------------------

BPM = 112; % Number of beats per minute in which the song was written for
MIN = 60; % Number of second in a minute (constant)
FS = 8000; % Sampling Rate (Hz)
delta = 1 / FS; % Seconds of each interval in the data set (1 / Hz)

qn = 0: delta : (MIN / BPM); % Timing of a quarter note (sec)
hn = 0: delta : (2 * (MIN / BPM)); % Timing of a a half note (sec)
en = 0: delta : (.5 * (MIN / BPM)); % Timing of a an eigth note (sec)
dq = 0: delta : (1.5 * (MIN / BPM)); % Timing of a a dotted quarter note (sec)

fA = 440; % Frequency of A (Hz)
fG = 2 ^ (-2 / 12) * fA; % Frequency of G (Hz)
fBf = 2 ^ (1 / 12) * fA; % Frequency of B-flat (Hz)
fC = 2 ^ (3 / 12) * fA; % Frequency of C (Hz)
fDf = 2 ^ (4 / 12) * fA; % Frequency of D-flat (Hz)

%--------------------------------------------------------------------------
% Part 1A: "Smoke on the Water" at original speed
%--------------------------------------------------------------------------

x = .02 * [sin(2*pi*fG*qn),sin(2*pi*fBf*qn),sin(2*pi*fC*dq),sin(2*pi*fG*qn),sin(2*pi*fBf*qn),sin(2*pi*fDf*en),sin(2*pi*fC*hn),sin(2*pi*fG*qn),sin(2*pi*fBf*qn),sin(2*pi*fC*dq),sin(2*pi*fBf*qn),sin(2*pi*fG*qn)];

%--------------------------------------------------------------------------
% Part 1B: "Smoke on the Water" played twice as fast. The signal in this
% case is not transformed. Rather, the notes are played at half their
% original length.
%--------------------------------------------------------------------------

BPM = 112 * 2; % This doubles the speed of the music

qn = 0: delta : (MIN / BPM); % Timing of a quarter note (sec)
hn = 0: delta : (2 * (MIN / BPM)); % Timing of a a half note (sec)
en = 0: delta : (.5 * (MIN / BPM)); % Timing of a an eigth note (sec)
dq = 0: delta : (1.5 * (MIN / BPM)); % Timing of a a dotted quarter note (sec)

x2 = .02 * [sin(2*pi*fG*qn),sin(2*pi*fBf*qn),sin(2*pi*fC*dq),sin(2*pi*fG*qn),sin(2*pi*fBf*qn),sin(2*pi*fDf*en),sin(2*pi*fC*hn),sin(2*pi*fG*qn),sin(2*pi*fBf*qn),sin(2*pi*fC*dq),sin(2*pi*fBf*qn),sin(2*pi*fG*qn)];

%--------------------------------------------------------------------------
% Part 1C: "Smoke on the Water" with transformation y(t) = x(2t). In This
% case, the original song (x) is time scaled. In addition to the speed
% increasing to twice the original speed, the frequencies increase as well.
%--------------------------------------------------------------------------

% This loop performs the required transformation by scaling the original
% signal to data points in the new signal
for n = 1 : (size(x,2) / 2)
y(n) = x(2 * n); % This is the matrix that holds the time scaled signal
end

%--------------------------------------------------------------------------
% Part 1 End: Write all three songs into a single sound file
%--------------------------------------------------------------------------

total = [x, x2, y]; % This combines all three parts into one matrix
sound(total, FS); % This plays all three parts at once
wavwrite(total, 'SmokeOnTheWater_MichaelJames.wav'); % Writes the sound file to a .wav

%--------------------------------------------------------------------------
% Part 2: Reversal of Beetles Song
%--------------------------------------------------------------------------

[original, fs] = wavread('beatles.wav'); % Reads the original "Beatles.wav" file with a sampling rate of fs
reversed = flipud(original); % Reverses the original matrix, thus reversing the song
wavwrite(reversed,fs,'BeatlesReversed_MichaelJames.wav'); % Writes the reversed file to a .wav file


Alumni Liaison

Ph.D. 2007, working on developing cool imaging technologies for digital cameras, camera phones, and video surveillance cameras.

Buyue Zhang