Making Cool Sounds with MATLAB (Like an Echo Effect!)
‘This assignments all about using a powerful tool called convolution in MATLAB to play with sound waves and create fun effets, I's
like mixing colors but with sounds!
\While this exercise focuses on sound, you can actually use convolution for many other things in engineering and science. But for now,
{gt ready to hear some cool sonic transformations!
Here's what you need:
+ Your trusted computer with MATLAB,
‘+ Apair of headphones or speakers (optional but highly recommended) to hear the awesome sounds you create.
Why headphones/speakers?
‘Wel, well be using MATLAB to play with sound and actually hear the results! MATLAB has builtin functions lke sound and sounds
that let you play sounds straight from your computer. Just type help. sound in MATLAB to learn more about these cool features.
Loading the Data
Toegin, acquire the speech waveform from the class webste named “echo.mat” Ensure that you save this datafile tothe curent
working directory of MATLAB. Fofow the steps below fo oad the data using the load command. This proces is crucial for our
subsequent analyses
1. Save “echo.mat” to your MATLAB'S currant working directory.
2, Open MATLAB on your computer
3, Type the following command inthe MATLAB command prompt
oad echo.mat
This command wil lad two essential variables into the MATLAB workspace:
+ 204: The speech waveform
+ £5: The sampling frequency ofthe speech waveform
whos
‘The whos command sisplays information about the variables in the current workspace, including their names, sizes, and data types.
Listening to the Speech Waveform
Given that you possess headphones, speakers, or access tothe internal speakers of your PC, you can auelbly perceive the speech
waveform using the sound or sounds functions in MATLAB. The prescribed syntax for these functions is as follows:
sound(xx, £5)
Executing this command will result in the playback ofthe speech waveform through your computer's sound card, Its worth noting that
the only distinction between sound and soundsc les in the scaling behavior of the waveform. Specifically, sounds is designed to
scalo the wavatorm optimally, maximizing volume without inducing clipping,
Plotting the Speech Waveform
Generate a graphical representation of the speech waveform by plting it against imo, rather than sample values. This entals
convering the sample values to their respective time values utilizing the provided sampling frequency. Employ the plot function fr this
purpose, as opposed othe stem function, given the transformation of he x ans to represent time instead of sample values
Ensure that al plots are appropriately labeled to facilitate a clear interpretation ofthe visualizations. This metculous approach
guarantees a comprehensive understanding ofthe speech waveform's temporal characteristics.
X calculate tine vector
time = (@:length(xx)-1) / fs;
X Plotting the Speech Waveform
figure;
plot(tine, x);
title’ -Echo- Speech Signal’);
xlabel("Tine (seconds)');
ylabel(‘Anplitude");Create a System Impulse Response
Create a system that mimics an “echo elect for the original speech waveform in discrete-time. The system should alow the current
Input value to pass trough witout atenuation (gain of 1). Addonally, I should incorporate a delayed version of te Input signal thats
attenuated by a factor of (the echo shoul be ithe sizeof the orginal signal and delayed by one second). In simpler tems, the
cutput ofthe system wil include both the unattenuated present signal and a delayed, atewated version
Imagine standing on one mountain and shouting towards another mountain - your voice reaches your ear instal, and a moment
later, you hear the echo from the distant mountain, In MATLAB, this translates to creating an impulse response that captures this,
behavior.
Consider the following hints:
‘+ Determine the number of samples in one second of time
+ When creating the impulse response, think about what the input should be. You're essentially seting up a scenario where the
system responds tothe immediate gound and the delayed, attenuated echo,
X Parameters
delay_seconds = 15 % One-second delay
attenuation factor = 1/4; % Attenuation factor for the echo
X Calculate delay in sanples
delay_saaples = round(éelay_seconds * fs);
X Create the impulse response
impulse_response = zeros(1, delay_sanples + 1);
Ampulse_response(1) = 1; X Unattenuated present value
impulse_response(delay_sanples + 1) = attenuation factor; % Attenuated delayed value
X Apply the system inpulse response using convolution (assuring you have loaded the data with variables xx and fs)
output waveform = conv(xx, inpulse_response, "full’)s
X Listening to the Output Waveform with Echo
soundsc(output_vaveform, s)5
X Plotting the Output Waveform
output_tine = (@:length(output_vaveforn)-1) / fs; % Time vector for output
Figure;
plot output_time, output_wavefors);
tAtLe( speech waveform with Echo");
xlabel("Time (seconds) ');
ylabel(‘Anplitude");
Exercise
Develop a function capable of handling four essential parameters:
+ Input signal
+ Time delay (in seconds)
+ Attenuation factor ofthe echo
+ Sampling frequency ofthe input signal
‘The function structure is exemplified as fllows:
[output] = cigitaldelay(input, medelay, echoattenuation, samplingtreq);
This function serves the purpose of passing both the immediate value of the inpu signal and producing an echo allenuated by a
specified factor. This effect is commonly employed in music, similar to providing a brief delay (approximately 0.1 seconds) to enhance
the richness of a guta’s sound. The outcome often emulates the impression of multiple guitars playing simultaneously.
Furthermore, the function is designed to offer lexiilly, enabling adjustments to both the time delay and the attenuation ofthe echo.
This versailty allows users to explore various settings, creating diverse auctlory experiences.
Now use this function to create a guitar effect known as digital delay on guitar mat file provided
Remember, the function should be written only at the bottom of alive script fie. Also, play around with different values of time delay andoad guitareat
hos:
ame size bytes Class attributes
scvenvation factor ma double
elay_sanpies ta double
elay_seconés ma 4 double
‘seroastenuation ma 4 doable
snoulse_response be22051 376408 double
output tine 24423196 3385568. double
feutput_havefors ans6a, 3385568 double
cine pase 328088 double
‘inedelay oa 8 double
m soto, 3200168 double
soundsc(xx, 5)
‘timedelay = 0.1; % Time delay in seconds
echoattenuation = @.5; % Attenuation factor of the echo
X Coll the digitaldelay function
‘output_signal = digitaldelay(xx, tinedelay, echoattenuation, fs);
X Play the guitar sound with digital delay effect
soundsc(output_signal, fs)
X Plot the input and output signals
‘time = (@:Length(x«)-1) / #55 % Tine vector
figure;
subplot(2, 1,1);
plot (eine, x);
title(’original Signal");
xlabel(‘Time (seconds) '
ylabel (‘Amplitude’);
subplot(2,1,2);
plot(tine, output signal);
title('signal with Digital Delay EFfect");
xlabel('Time (seconds) ');
ylabel( ‘Amplitude’;
Original Signal
o 1 2 34 6 6 7 @ @ 0
Tne (sovonds)
‘Signal with Digital Delay Etfect
i =
og
‘anisaed
function output = digitaldelay(input, tinedelay, echoat tenvation, samplingéreq)end
X Validate input arguments
if margin ~= 4
error(*Four input argunents are required.");
end
delay_sazples = round(tiredelay * saeplingfrea);
delayed signal = [zeros(delay_sanples, 1); input];
delayed_signal(1:delay_sanples) = echoattenuation * delayed_signal(1:delay_samples);
output = delayed_signal(izlength(input));