Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, can you help me please. I\'m having trouble running this matlab script an

ID: 2249035 • Letter: H

Question

Hello, can you help me please. I'm having trouble running this matlab script and am not sure if I wrote the code correctly. I would like Matlab to play the song "Twinkle Twinkle Little Star", and then run the speed at double and half speed, fade in and out, and play it in reverse. Can you please review the script and make any needed revisions. Thank you so much!

clear all; close all;

% MATLAB script to generate the song "Twinkle Twinkle Little Star"

% and perform audio editing

%% make music

% note frequencies used in the song in Hz

fc4 = 262.63;

fd4 = 293.66;

fe4 = 329.63;

% DEFINE the frequencies ff4, fg4, fa4 for notes f4, g4 and a4

% based on the note frequency table in the lab manual

one_beat = 0.5; % time duration of a one-beat note in sec

fs = 44100; % sampling frequency in Hz

t = 0:1/fs:one_beat; % time vector for a one-beat note

t1 = 0:1/fs:2*one_beat; % time vector for a two-beat note

% generate sound wave sinusoids at the note frequencies

% one-beat notes

c4 = sin(2*pi*fc4*t);

d4 = sin(2*pi*fd4*t);

e4 = sin(2*pi*fe4*t);

% GENERATE sinusoids for f4, g4 and a4

% two-beat notes

g4_2 = sin(2*pi*fg4*t1);

c4_2 = sin(2*pi*fc4*t1);

d4_2 = sin(2*pi*fd4*t1);

% create melody vectors for Twinkle Twinkle Little Star

melody1 = [c4, c4, g4, g4, a4, a4, g4_2];

melody2 = [f4, f4, e4, e4, d4, d4, c4_2];

melody3 = [g4, g4, f4, f4, e4, e4, d4_2];

song = [melody1, melody2, melody3, melody3, melody1, melody2];

% write the song to a file and can be played later using a music player

audiowrite('twinkle_twinkle.wav', song, fs);

soundsc(song, fs) % play the song

pause % need to press any key to continue

%% speed up the song (twice of original speed)

soundsc(song, 2*fs)

pause

%% WRITE code to slow down the song (half of original speed)

soundsc(song, 0.5*fs)

pause

%% fade in and fade out

% fade in the beginning "twinkle, twinkle, little star, how I wonder what you are" and

% fade out in the end "twinkle, twinkle, little star, how I wonder what you are"

fade_in_time = 2; % fade in time in sec

fade_in_num_sample = fade_in_time*fs; % number of samples in fade-in

fade_in_factor = 0:1/(fade_in_num_sample-1):1; % linear fade

fade_in_seg = song(1:fade_in_num_sample).*fade_in_factor; % scale

fade_out_time = 2; % fade out time in sec

fade_out_num_sample = fade_out_time*fs; % number of samples in fade-out

fade_out_factor = 1:-1/(fade_out_num_sample-1):0; % linear fade

% end: last index of an array

fade_out_seg = song(end-fade_out_num_sample+1 : end).*fade_out_factor; % scale

% the middle unchanged segment

middle_seg = song(fade_in_num_sample+1 : end-fade_out_num_sample);

song_faded = [fade_in_seg, middle_seg, fade_out_seg];

soundsc(song_faded, fs)

pause

%% play in reverse

% WRITE the code to play the song in reverse, hint: fliplr function

fliplr(song, fs)

pause

Explanation / Answer

The version of code given below works properly. Please note that dueto pause statement the matlab pauses the execution to run next segment of code you need to click on command window and hit any key from keyboard.

---------------------------------------------------------------------------------------------------------------------------------------------------------

clear all; close all;
% MATLAB script to generate the song "Twinkle Twinkle Little Star"
% and perform audio editing
%% make music
% note frequencies used in the song in Hz
fc4 = 262.63;
fa4 = 440.00;
fd4 = 293.66;
fe4 = 329.63;
fg4 = 392.00;
% DEFINE the frequencies ff4, fg4, fa4 for notes f4, g4 and a4
% based on the note frequency table in the lab manual

% time duration of a one-beat note in sec
fs = 44100; % sampling frequency in Hz
t = 0:1/fs:one_beat; % time vector for a one-beat note
t1 = 0:1/fs:2*one_beat; % time vector for a two-beat note

% generate sound wave sinusoids at the note frequencies
% one-beat notes
a4 = sin(2*pi*fa4*t);
c4 = sin(2*pi*fc4*t);
d4 = sin(2*pi*fd4*t);
e4 = sin(2*pi*fe4*t);
g4 = sin(2*pi*fg4*t);
f4 = sin(2*pi*fc4*t);
% GENERATE sinusoids for f4, g4 and a4

% two-beat notes
g4_2 = sin(2*pi*fg4*t1);
c4_2 = sin(2*pi*fc4*t1);
d4_2 = sin(2*pi*fd4*t1);

% create melody vectors for Twinkle Twinkle Little Star
melody1 = [c4, c4, g4, g4, a4, a4, g4_2];
melody2 = [f4, f4, e4, e4, d4, d4, c4_2];
melody3 = [g4, g4, f4, f4, e4, e4, d4_2];

song = [melody1, melody2, melody3, melody3, melody1, melody2];

% write the song to a file and can be played later using a music player
audiowrite('twinkle_twinkle.wav', song, fs);

soundsc(song, fs) % play the song
pause % need to press any key to continue


%% speed up the song (twice of original speed)
soundsc(song, 2*fs)
pause

%% WRITE code to slow down the song (half of original speed)
soundsc(song, 0.5*fs)
pause

%% fade in and fade out
% fade in the beginning "twinkle, twinkle, little star, how I wonder what you are" and
% fade out in the end "twinkle, twinkle, little star, how I wonder what you are"
fade_in_time = 2; % fade in time in sec
fade_in_num_sample = fade_in_time*fs; % number of samples in fade-in
fade_in_factor = 0:1/(fade_in_num_sample-1):1; % linear fade
fade_in_seg = song(1:fade_in_num_sample).*fade_in_factor; % scale

fade_out_time = 2; % fade out time in sec
fade_out_num_sample = fade_out_time*fs; % number of samples in fade-out
fade_out_factor = 1:-1/(fade_out_num_sample-1):0; % linear fade
% end: last index o f an array
fade_out_seg = song(end-fade_out_num_sample+1 : end).*fade_out_factor; % scale
% the middle unchanged segment
middle_seg = song(fade_in_num_sample+1 : end-fade_out_num_sample);

song_faded = [fade_in_seg, middle_seg, fade_out_seg];
soundsc(song_faded, fs)
pause

%% play in reverse
% WRITE the code to play the song in reverse, hint: fliplr function

fliplr(song, fs)
pause

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote