Line 117: | Line 117: | ||
''[[File:convolution_experiment_src.zip|Download Source Package]]'' | ''[[File:convolution_experiment_src.zip|Download Source Package]]'' | ||
+ | |||
+ | ===Experiment Procedure=== | ||
+ | The overall procedure includes recording my normal voice, recording the impulse response, trimming the impulse response, convolving the two audio signals, and finally playing the output. | ||
+ | |||
+ | Detailed procedure: | ||
+ | * In the command window in Matlab, run the ''recordToFile'' function and record your voice for any length of time. | ||
+ | * Record the impulse response of the cactus cup by calling the ''recordToFile'' function for about 1 second, using a different file name. During the 1 second of recording, | ||
+ | * |
Revision as of 22:14, 2 December 2018
Contents
Application of LTI Systems and Convolution in Matlab
Background
Often music groups would like to make their studio recordings sound as if they were played in a live venue. One method to achieve this is to convolve the audio recording with an impulse response taken from the concert venue, or another location with similar acoustics.
Convolution may also be used in the same way to alter your voice which will be demonstrated below. All you need to try out the experiments are a laptop with microphone, Matlab student edition or better, and a couple everyday items.
Audio Impulse Response
We have seen impulse responses expressed as mathematical functions such as $ 2^{-n}u[n] $. Such functions we compute convolutions with in class may model actual impulse responses such as the fading echo heard after clapping in a concert hall or ringing of a gong after being struck. To obtain a good impulse response recording, audio data should include the sound from just after the impulse until it has died away sufficiently. Distortion of the output may result from convolving with an incorrectly recorded impulse response.
Voice Effect Experiment
I will demonstrate the procedure for creating voice effects with convolution with a Matlab experiment. I will attempt to make my voice sound as if I am speaking into an empty cactus cup (large plastic cup), by convolving my normal voice with the impulse response of the cup.
The Code
Below are line numbered code listings for the required Matlab helper functions.
Function for recording X seconds of audio.
function [ ] = recordToFile( inFileName, Fs, nbits, seconds )
%record audio to a file with specified name, record for specified time,
%and at the sampling frequency and bit size (8,16,or 24)
recordObj = audiorecorder(Fs, nbits, 1);
record(recordObj);
pause(seconds);
stop(recordObj);
audiowrite(inFileName, getaudiodata(recordObj), Fs);
end
Function for trimming X seconds audio from the back end of the audio data
function [ lengthSec ] = trimAudioEnd( inFileName, seconds )
%trimAudioEnd: truncate audio file by x seconds, output the
%file pointer and the length in seconds of audio contained in the file now.
%param inFileName: the name of the audiofile
%param seconds: seconds of audio to trim from the end
[y,fs] = audioread(inFileName);
if mod(seconds*fs, 1) ~= 0
error('sample frequency times seconds must be an integer')
end
samples = [1, length(y) - (seconds*fs)];
[y1,fs] = audioread(inFileName, samples);
audiowrite(inFileName, y1, fs);
lengthSec = length(y1) / fs;
end
Function for trimming X seconds of audio from the front end of the audio data
function [ lengthSec ] = trimAudioFront( inFileName, seconds )
%trimAudioFront: cut out the first x seconds of audio, output the
%file pointer and the length in seconds of audio contained in the file now.
%param inFileName: string that is the name of the audio file
%param seconds rational floating point amount of time, must be a multiple
%of the frequency at which the input audio was sampled
%error checking
[y,fs] = audioread(inFileName);
if mod(seconds*fs, 1) ~= 0
error('sample frequency times seconds must be an integer')
end
%steps:
%1. flip the audio data in y array
%2. write data to the inFile
%3. call trimAudioEnd on the aufio file
%4. flip the data of the trimmed file back
y_copy = y;
for k = 1:length(y)
y(k) = y_copy(length(y) - (k-1));
end
audiowrite(inFileName, y, fs);
trimAudioEnd(inFileName,seconds);
[y1,fs] = audioread(inFileName);
y1_copy = y1;
for k = 1:length(y1)
y1(k) = y1_copy(length(y1) - (k-1));
end
audiowrite(inFileName,y1,fs);
lengthSec = length(y1) / fs;
end
Function to play an audio file supported by Matlab
function [ ] = playAudioNormal( inFileName )
%playAudioNormal: playback an audio file at sampling rate with which it was
%sampled
%param inFileName: string name('') of the audio file to play
[y,fs] = audioread(inFileName);
f = audioread(inFileName);
sound(f,fs)
end
function [ ] = convolveAudio(inputAudioFileName, impulseResponseFileName, outputFileName, Fs)
%colvolveAudio: compute the convolution of the audio signals stored in two
%files and create a new audio file containing the output audio signal
%param Fs: the frequency that the input audio signals were sampled at
%param outputFileName: name of the output audio file
%param impulseResponseFileName: name of the trimmed impulse response audio
%file
%param inputAudioFileName: name of the input audio file to the system
x = audioread(inputAudioFileName);
h = audioread(impulseResponseFileName);
y = conv(x,h);
audiowrite(outputFileName, y, Fs);
end
File:Convolution experiment src.zip
Experiment Procedure
The overall procedure includes recording my normal voice, recording the impulse response, trimming the impulse response, convolving the two audio signals, and finally playing the output.
Detailed procedure:
- In the command window in Matlab, run the recordToFile function and record your voice for any length of time.
- Record the impulse response of the cactus cup by calling the recordToFile function for about 1 second, using a different file name. During the 1 second of recording,