You are on page 1of 4

Báo cáo thực hành

AUDIO RECORDING IN MATLAB


42101296_Nguyễn Hoàng Duy
42101418_Tô Nguyễn Thịnh Phát

I. Introduction
The Dirac delta function
The Dirac delta function (or δ distribution), also known as the unit impulse, is a
generalized function on the real numbers, whose value is zero everywhere except at zero,
and whose integral over the entire real line is equal to one.
The Dirac delta function delta dirac can be loosely thought of as a function on the real
line which is zero everywhere except at the origin, where it is infinite,

Audio Recording in Matlab


To record audio in MATLAB, you can use the audiorecorder object along with the record
and ‘stop’ functions. Here's a basic example:
This code will record audio for 5 seconds at a sampling frequency of 44100 Hz (standard
for CD quality audio). Adjust the fs and duration variables according to your
requirements.
Here's a breakdown of the steps:
1. Define the recording parameters such as sampling frequency (fs) and duration.
2. Create an audiorecorder object specifying the sampling frequency, bits per sample,
and number of channels.
3. Start recording using the recordblocking function, which records audio for a
specified duration.
4. Retrieve the recorded audio data using the getaudiodata function.
5. Optionally, play the recorded audio using the sound function.
6. Optionally, plot the recorded waveform for visualization.
Remember to have a microphone connected to your computer for recording. Adjust the
recording device settings if necessary using the recorderinfo function.
II. Lab Procedure
AUDIO RECORDING CONVERT TO DIRAC DELTA
clc;
clear all;
fs=8000; Channels=1; bits=16;
r=audiorecorder (fs,bits,Channels);
duration=5; disp('Recording started');
recordblocking(r,duration);
disp('Recording stopped');
x=getaudiodata(r);
t=0:1/fs:(length(x)-1)/fs;
subplot(2,1,1); plot(t,x,'LineWidth',1.5);
xlabel('time (sec)'); ylabel('Amplitude');
title('Time Domain Plot of the Recorded Signal');

n=length(x);
F=0:(n-1)*fs/n;
Y=fft(x,n);
F0=(-n/2:n/2-1).*(fs/n);
Y0=fftshift(Y);
AY=abs(Y0);
subplot(2,1,2); plot(F0,AY,'LineWidth',1.5');
xlabel('Frequency (Hz)'); ylabel('Amplitude');
title('Frequency Domain Plot of Audio Signal');
filename='speech.wav';
audiowrite(filename,x,fs);
%sound(x,fs,bits);

EXPLAIN: This MATLAB code records audio from the computer's microphone, then
plots the waveform and frequency domain of the recorded audio signal. Here's the
breakdown:
1. clc; clear all;: Clears the command window and removes all variables from the
MATLAB workspace to start with a clean working environment.
2. fs=8000; Channels=1; bits=16;: Sets the recording parameters, including the
sampling frequency (fs), number of channels (Channels), and bits per sample
(bits).
3. r=audiorecorder(fs,bits,Channels);: Creates an audiorecorder object with the
specified parameters.
4. duration=5; disp('Recording started'); recordblocking(r,duration); disp('Recording
stopped');: Begins recording for 5 seconds and displays recording status messages.
5. x=getaudiodata(r);: Retrieves the recorded audio data from the audiorecorder
object and stores it in the variable x.
6. t=0:1/fs:(length(x)-1)/fs;: Generates the time vector t corresponding to the samples
in the audio data.
7. subplot(2,1,1); plot(t,x,'LineWidth',1.5); xlabel('time (sec)'); ylabel('Amplitude');
title('Time Domain Plot of the Recorded Signal');: Plots the waveform of the audio
signal in the time domain.
8. n=length(x); F=0:(n-1)*fs/n; Y=fft(x,n); F0=(-n/2:n/2-1).*(fs/n); Y0=fftshift(Y);
AY=abs(Y0); subplot(2,1,2); plot(F0,AY,'LineWidth',1.5'); xlabel('Frequency
(Hz)'); ylabel('Amplitude'); title('Frequency Domain Plot of Audio Signal');:
Performs the Fourier transform of the audio signal and plots the corresponding
frequency domain.
9. filename='speech.wav'; audiowrite(filename,x,fs);: Saves the recorded audio signal
to a WAV file named 'speech.wav'.
10. %sound(x,fs,bits);: This line is commented out (using %), otherwise it would play
back the recorded audio within MATLAB.
This code provides a simple way to record audio from a microphone and visualize both
the waveform and frequency domain of the recorded audio signal.
III. Results

You might also like