Line 50: | Line 50: | ||
Function for trimming audio from the front end of the audio data | Function for trimming audio from the front end of the audio data | ||
<code><syntaxhighlight lang="MATLAB" line='line'> | <code><syntaxhighlight lang="MATLAB" line='line'> | ||
− | function [ | + | function [ lengthSec ] = trimAudioFront( inFileName, seconds ) |
%trimAudioFront: cut out the first x seconds of audio, output the | %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. | %file pointer and the length in seconds of audio contained in the file now. | ||
Line 80: | Line 80: | ||
end | end | ||
audiowrite(inFileName,y1,fs); | audiowrite(inFileName,y1,fs); | ||
+ | lengthSec = length(y1) / fs; | ||
end | end | ||
</syntaxhighlight></code> | </syntaxhighlight></code> |
Revision as of 21:32, 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 functions.
Function for recording 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 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 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