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

I need to write a short matlab code to do the following please help. Using Matla

ID: 3909186 • Letter: I

Question

I need to write a short matlab code to do the following please help.

Using Matlab Create 5 seconds of pleasant sound. Use the "Making Music" slides to guide you. Create a *.wav file (or another common format) so I can listen to the pleasant sound. NOTE: One important thing you need to know (not really understand) is that sampling frequency must but be twice the highest frequency you are trying to create. For example, in my sound "Charge" (that I played for you in class) the highest frequency is the note known as Middle G (on a piano, a.ka Ga) The frequency of this note is 392 Hz. So if I want to create this note using Matlab, I would have to use a sampling rate of at least 784 Hz. I would not be able to create any frequencies higher than 392 Hz.

Explanation / Answer

clear all
clc

% sampling frequency almost twice of the frequency
fs = 784;
% Declaring the frequency value
frequency = 392;
% duration of 5 seconds
duration = 5;
% declaring the amplitude
amp = 15;
% calculating the values
values = (0 : 1/fs : duration);
% creating the sound matrix
mySound = amp * sin(2 * pi* frequency * values);
% normalizing the sound matrix to make in range of
% 16 bit precision
max_val = max(abs(mySound));
mySound = mySound./max_val;

% declaring the file name to save the audio
filename = 'myWavFile.wav';
% writing the audio to file "myAudioFile.wav"
audiowrite(filename, mySound, fs);
% reading the audio file
[mytune, Fs] = audioread(filename);
% playing the sound
disp('Make sure your speakers are on.')
disp('Playing the sound...')
sound(mytune);

Important Instruction: Newer Matlab versions do not support wavwrite(), and wavread() instead they support audiowrite() and audioread() respectively. As the program is developed in Matlab R2017a hence the newer functions has been used. So if you have older version of Matlab then probably you will get an error when you try to run the program so please replace the function audiowrite() with wavwrite() and audioread() with wavread() to run the program correctly.

Matlab Code

clear all
clc

% sampling frequency almost twice of the frequency
fs = 784;
% Declaring the frequency value
frequency = 392;
% duration of 5 seconds
duration = 5;
% declaring the amplitude
amp = 15;
% calculating the values
values = (0 : 1/fs : duration);
% creating the sound matrix
mySound = amp * sin(2 * pi* frequency * values);
% normalizing the sound matrix to make in range of
% 16 bit precision
max_val = max(abs(mySound));
mySound = mySound./max_val;

% declaring the file name to save the audio
filename = 'myWavFile.wav';
% writing the audio to file "myAudioFile.wav"
audiowrite(filename, mySound, fs);
% reading the audio file
[mytune, Fs] = audioread(filename);
% playing the sound
disp('Make sure your speakers are on.')
disp('Playing the sound...')
sound(mytune);