%% Bandstop Filtering of Musical Signal
% Implement a basic digital music synthesizer and use it to play a traditional song. Specify a sample rate of 2 kHz. Plot the spectrogram of the song.

fs = 2e3;
t = 0:1/fs:0.3-1/fs;

l = [0 130.81 146.83 164.81 174.61 196.00 220 246.94];
m = [0 261.63 293.66 329.63 349.23 392.00 440 493.88];
h = [0 523.25 587.33 659.25 698.46 783.99 880 987.77];
note = @(f,g) [1 1 1]*sin(2*pi*[l(g) m(g) h(f)]'.*t);

mel = [3 2 1 2 3 3 3 0 2 2 2 0 3 5 5 0 3 2 1 2 3 3 3 3 2 2 3 2 1]+1;
acc = [3 0 5 0 3 0 3 3 2 0 2 2 3 0 5 5 3 0 5 0 3 3 3 0 2 2 3 0 1]+1;

song = [];
for kj = 1:length(mel)
    song = [song note(mel(kj),acc(kj)) zeros(1,0.01*fs)];
end
song = song/(max(abs(song))+0.1);

% To hear, type sound(song,fs)

pspectrum(song,fs,'spectrogram','TimeResolution',0.31, ...
    'OverlapPercent',0,'MinThreshold',-60)

%
% Bandstop-filter the signal to separate the middle register from the other two. Specify passband frequencies of 230 Hz and 450 Hz. Plot the original and filtered signals in the time and frequency domains.

bong = bandstop(song,[230 450],fs);

% To hear, type sound(bong,fs)

bandstop(song,[230 450],fs)

%
% Plot the spectrogram of the song without the middle register.

figure
pspectrum(bong,fs,'spectrogram','TimeResolution',0.31, ...
    'OverlapPercent',0,'MinThreshold',-60)

% Copyright 2012 The MathWorks, Inc.