Question
Please i need help, so confused with this question. Download the file BE401 2017 HW5P6.m , and run it on MATLAB to make sure you
Please i need help, so confused with this question.
Download the file BE401 2017 HW5P6.m, and run it on MATLAB to make sure you can both record and play audio. For best effect, you should use headphones. The code should also produce a plot of the impulse response h(t). Read through the code, making sure you understand the meaning of the parameters, and how the impulse response creation follows the description above. The origin for spatial coordinates is the lower left corner of the room containing the listener.
Answer the below questions. You may include printouts of figures if you wish, but it is not required.
Here's the BE401 2017 HW5P6.m MATLAB file:
% Code for homework exercise on how to simulate a reverberant room.
%
%Uses cell mode: in the Matlab editor, place the cursor within a
% cell (a new cell begins each line that starts with a double comment %%),
% and press Cmd-Enter or Ctrl-Enter to execute only the code within that
% cell. In particular, once the impulse response is created, it can
% be applied repeatedly to new sounds by running only the recording and
% playback cells.
%
% Students should first try running the full Mfile as is, to make sure they
% can record and play audio on their device. The homework asks students to
% modify parameters that define an impulse response that simulates the
% effects of sound reflection off walls in a room. As explained in the
% exercise, we will use the virtual source (also known as image) method,
% which treats each reflected sound wave as coming from a virtual source
% outside the room.
%
% Students should use headphones to be able to hear the effects of
% parameter changes.
%
% -------- _ ------------
% /--> | Reverb | --> snd_rev, y(t) ->|+|-> | Headphones |
% | -------- ^ ------------
% ------------ | |
% | Microphone | --|--> snd_src, x(t) ------------------/
% ------------
close all % If we run this cell we are probably doing a "full" reset
clear all
%------------------------------------------------------------------------
%% Set parameters
%
% You will experiment with parameters as part of the exercise
% Audio parameters
record_dur = 3; % Time in seconds to record
sample_rate = 44100; % Sampling rate in Hz, both record and playback
% Physical parameters
L = 80; % room length, meters
W = 80; % room width, meters
c = 345; % speed of sound, m/s
beta = 0.9; % Fraction of sound amplitude reflected, same for all walls
position_source = [9 7]; % [L W] in meters
position_listener = [2 1]; % [L W], in meters
% Impulse response parameters
IR_dur = 0.6; % (Approximate) impulse response duration, seconds
% Should be long enough to include echoes with non-negligible
% power, but longer IRs increase computational load
IR_length = round(IR_dur*sample_rate); % Number of samples in IR
block_radius = 0; % Range of 2x2 "blocks" in each direction to check.
% Can be any non-negative integer
% Number of calculated sources is 4*(2*block_radius+1)^2.
% The longer IR_dur, the larger block_radius should be.
%------------------------------------------------------------------------
%% Set up audio recording and playback
%
% You should not need to change the code in this cell.
% Helper function to normalize sound levels:
good_pwr = 0.05^2; % 0 normalizep = @(x) max(-1,min(1,sqrt(good_pwr/var(x))*x)); % Apply to signals before playback % Set up ability to record audio SNDREC = audiorecorder(sample_rate,8,1); % Create recording object pause(1) % Might glitch if recording starts too soon after creation %------------------------------------------------------------------------ %% Record raw audio (sound at the source) % % You can place the cursor in this cell and hit Ctrl-Return or Cmd-Return % repeatedly to record new audio as source sound wave, and simulate the % sound at the listener position. % % You should not need to change the code in this cell. disp('Start of recording...'); recordblocking(SNDREC, record_dur); disp('End of recording.'); % Get the data for audio at the source snd_src = getaudiodata(SNDREC); %------------------------------------------------------------------------ %% Create the impulse response % % There are many optimizations used in professional audio simulators. For clarity, we % will use a brute force approach, that loops through 2x2 blocks of virtual rooms. % Each of the 4 virtual rooms in each block contains one virtual source, that contributes % a delayed and attenuated copy of the original sound to the listener position. Each % copy arises from a separate impulse in h. % % For each source, we thus need to find the travel time (to determine which % index in h to place an impulse) and the distance and number of walls crossed (to % determine the attenuation). disp('Making impulse response...') h = zeros(1,IR_length); % Initialize impulse response vector for n = -block_radius:block_radius for m = -block_radius:block_radius block_index = [2*n 2*m]; % Which 2x2 block of rooms are we at? % Top left room % What is distance from virtual source to listener? R = norm(block_index.*[L W] + [-1 1].*position_source - position_listener); % R/c gives travel time, convert to sample number in h array echo_ind = round((R/c)*sample_rate); % Can be zero % How many "walls" are there between virtual source and listener? num_reflections = sum(abs(block_index+[-1 0])); % Now add a scaled impulse at that index in h if echo_ind h(1+echo_ind) = h(1+echo_ind) + 1/(4*pi*R)*(beta^num_reflections); end % Remaining rooms are treated similarly % Top right room R = norm(block_index.*[L W] + [1 1].*position_source - position_listener); echo_ind = round((R/c)*sample_rate); num_reflections = sum(abs(block_index+[0 0])); if echo_ind h(1+echo_ind) = h(1+echo_ind) + 1/(4*pi*R)*(beta^num_reflections); end % Bottom left room R = norm(block_index.*[L W] + [-1 -1].*position_source - position_listener); echo_ind = round((R/c)*sample_rate); num_reflections = sum(abs(block_index+[-1 1])); if echo_ind h(1+echo_ind) = h(1+echo_ind) + 1/(4*pi*R)*(beta^num_reflections); end % Bottom right room R = norm(block_index.*[L W] + [1 -1].*position_source - position_listener); echo_ind = round((R/c)*sample_rate); num_reflections = sum(abs(block_index+[0 1])); if echo_ind h(1+echo_ind) = h(1+echo_ind) + 1/(4*pi*R)*(beta^num_reflections); end end % for m end % for n % Plot impulse response figure(1000) set(1000,'WindowStyle','docked') plot((0:length(h)-1)/sample_rate,h) xlabel('Time (sec)') ylabel('Impulse response h(t)') disp('...done') %------------------------------------------------------------------------ %% Simulate the reverberant sound at listener % % You can place the cursor in this cell and hit Ctrl-Return or Cmd-Return % repeatedly to record new audio as source sound wave, and simulate the % sound at the listener position. % % Tip: using cell mode, you can change simulation parameters, generate the % new h above, and apply it here, without recording a new audio sample each % time. % % You should not need to change the code in this cell. % Compute convolution y=h*x to simulate listener audio signal snd_rev = conv(snd_src,h); % Why do we not need to multiply by dt? % Make playback objects for sound at source and listener positions obj_src = audioplayer(normalizep(snd_src), sample_rate); obj_rev = audioplayer(normalizep(snd_rev), sample_rate); %------------------------------------------------------------------------ %% Playback % % You can place the cursor in this cell and hit Ctrl-Return or Cmd-Return % repeatedly to listen to the difference between sound at the source and % listener positions. % % You should not need to change the code in this cell % Play sound at source input(' disp('Playing original...') playblocking(obj_src) % Play reverberant sound at listener (if object exists) if any(h(:)~=0) disp('Playing reverb signal...') playblocking(obj_rev) end figure(1000) % Pop focus on figure
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started