0% found this document useful (0 votes)
9 views6 pages

DSP Lab 2

This document outlines a MATLAB script for recording audio, saving it, and analyzing its time and frequency domain characteristics. It includes steps for creating an audiorecorder object, plotting the waveform, computing the Fourier Transform, and generating a spectrogram using the Short-Time Fourier Transform (STFT). The script utilizes a Hann window for segmenting the audio data during the STFT process.

Uploaded by

Paul Phineas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

DSP Lab 2

This document outlines a MATLAB script for recording audio, saving it, and analyzing its time and frequency domain characteristics. It includes steps for creating an audiorecorder object, plotting the waveform, computing the Fourier Transform, and generating a spectrogram using the Short-Time Fourier Transform (STFT). The script utilizes a Hann window for segmenting the audio data during the STFT process.

Uploaded by

Paul Phineas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DSP LAB 2

Code:
fs = 8000; % Sampling frequency
nBits = 16; % Bit depth
nChannels = 1; % Number of audio channels
ID = -1; % Default audio input device

% Create an audiorecorder object


recObj = audiorecorder(fs, nBits, nChannels, ID);

% Start recording
disp("Begin speaking.")
recordblocking(recObj, 2); % Record for 2 seconds
disp("End of recording.")

% Retrieve audio data


x = getaudiodata(recObj, 'uint8');

% Save the recorded audio to a temporary directory


tempDir = tempdir; % Get the temporary directory
filename = fullfile(tempDir, 'jambo.wav'); % Create the full path
audiowrite(filename, x, fs);

% Play the recorded audio


play(recObj);
[x, fs] = audioread(filename); % Read the audio file
t = linspace(0, length(x) / fs, length(x)); % Generate time vector
figure(1)
plot(t, x) % Plot waveform against time
title('Time Domain Plot of "jambo.wav"')
xlabel('Time (s)')
ylabel('Amplitude')

N = length(x); % Number of points in FFT


F = fft(x, N); % Compute Fourier Transform
f = (0:N-1)*(fs/N); % Frequency range

figure(2)
plot(f, abs(F)) % Plot magnitude spectrum
title('Frequency Domain of "jambo.wav"')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
xlim([0 fs/2]) % Limit to half the sampling rate

N = 256; % Window length


overlap = 128; % Overlap between windows
hann_window = 0.5 * (1 - cos(2 * pi * (0:N-1)' / (N-1))); % Create Hann window manually

% Perform the Short-Time Fourier Transform (STFT) manually


len_x = length(x);
step = N - overlap;
num_segments = floor((len_x - overlap) / step);
spectrogram_matrix = zeros(N/2+1, num_segments);
windowed_segment = zeros(N, 1);
% Loop over segments and compute FFT
for i = 1:num_segments
start_idx = (i-1) * step + 1;
end_idx = start_idx + N - 1;
if end_idx > len_x
break;
end
windowed_segment = x(start_idx:end_idx) .* hann_window;
fft_segment = fft(windowed_segment);
spectrogram_matrix(:, i) = abs(fft_segment(1:N/2+1));
end

% Display the spectrogram


figure(3)
imagesc([0 size(spectrogram_matrix, 2)], [0 fs/2], 20*log10(spectrogram_matrix + 1e-6))
axis xy
colorbar
xlabel('Time (s)')
ylabel('Frequency (Hz)')
title('Spectrogram of "jambo.wav"')

You might also like