MATLAB Examples


Example usage of SDM toolbox for analysis, synthesis, and multi-channel reproduction.

The data are room impulse responses measured in a living room. Sakari Tervo & Jukka Pätynen, Aalto University, 2016 and Copyleft

% SDM toolbox : demoDefaultSetups
% Sakari Tervo & Jukka Pätynen, Aalto University, 2016
% and

Load the impulse response and the source signal

1s long impulse response measured at 192 kHz IRs contains left and right channels loudspeakers are large size active loudspeakers ( Genelec something ) Distance to loudspeakers about > 2 meters

% Download a spatial room impulse response
ir_filename = 'IR_living_room';
if ~exist([ir_filename '.mat'],'file')
     disp(['Downloading an example IR ' ir_filename ' from the database.'])
    url_ir = ['' ir_filename '.mat'];
    websave([ir_filename '.mat'],url_ir);

Download a stereofile (originally from free music archive)

audio_filename = 'paper_navy_swan_song';
if ~exist([audio_filename ,'.mp3'],'file')
    disp('Downloading an example music file from free music archive.')
    url_of_the_song = '';
    outfilename = websave([audio_filename '.mp3'],url_of_the_song);

% If websave not supported, you have to download IRs and source signals
% manually from the urls given below
% ''
% ''

Read the data

Read impulse response

load([ir_filename '.mat'])
% Read stereo signal
S = audioread([audio_filename '.mp3']);

Create SDM struct for analysis with a set of parameters

Parameters required for the calculation Load default array and define some parameters with custom values

fs = 192e3;
a = createSDMStruct('DefaultArray','GRASVI25','fs',fs);
User-defined SDM Settings are used :
           fs: 192000
            c: 345
       winLen: 0
    parFrames: 8192
    showArray: 0
      micLocs: [6x3 double]

Calculate the SDM coefficients

Solve the DOA of each time window assuming wide band reflections, white noise in the sensors and far-field (plane wave propagation model inside the array)

DOA{1} = SDMPar(ir_left, a);

% Here we are using the top-most microphone as the estimate for the
% pressure in the center of the array
P{1} = ir_left(:,5);

% Same for right channel
DOA{2} = SDMPar(ir_right, a);
P{2} = ir_right(:,5);
Started SDM processing
Using frame size 36
Ended SDM processing in 5.6879 seconds.
Started SDM processing
Using frame size 36
Ended SDM processing in 5.5701 seconds.

Create a struct for visualization with a set of parameters

Load default setup for very small room and change some of the variables

v = createVisualizationStruct('DefaultRoom','VerySmall',...
    'name','My Living Room','fs',fs);
% For visualization purposes, set the text interpreter to latex
createVisualizationStruct : User-defined visualization settings are used :
              fs: 192000
           plane: 'lateral'
             DOI: 'backward'
       plotStyle: 'fill'
            name: 'My Living Room'
             res: 1
               t: [0 2 5 20]
          colors: [4x3 double]
       dBSpacing: [-12 -6 0]
      DOASpacing: 30
       linewidth: [0 0 0 0]
        showGrid: 1
      dBDynamics: 36
    smoothMethod: 'average'
       smoothRes: 3

Draw analysis parameters and impulse responses

parameterVisualization(P, v);
Started visualization of parameters.
Ended visualization of parameters in 0.15861 seconds.

Draw time frequency visualization

Drawing only the lateral plane

timeFrequencyVisualization(P, v)
Started time-frequency visualization.
Ended time-frequency visualization in 7.6581 seconds.

Draw the spatio temporal visualization

spatioTemporalVisualization(P, DOA, v)
Started spatio-temporal visualization.
Ended spatio-temporal visualization in 2.2542 seconds.

Create synthesis struct with the given parameters

Load default 5.1 setup and define some parameters with custom values

s = createSynthesisStruct('defaultArray','5.1',...
% You always need to define 'snfft'
createSynthesisStruct: User-defined settings are :
                fs: 192000
                 c: 343
    dimensionality: 2
             snfft: 192000
         showArray: 0
            Radius: 2
         LFEcutOff: 80
          Binaural: 0
           HRTFset: 3
                 g: [384000x10 double]
                f1: [10x1 double]
               Blp: [2.2371e-09 6.7112e-09 6.7112e-09 2.2371e-09]
               Alp: [1 -2.9948 2.9895 -0.9948]
               Bhp: [0.9974 -2.9922 2.9922 -0.9974]
               Ahp: [1 -2.9948 2.9895 -0.9948]
           lspLocs: [6x3 double]
        LFEchannel: 4

Synthesize the spatial impulse response with NLS

H = cell(1,2);
for channel = 1:2
    H{channel} = synthesizeSDMCoeffs(P{channel},DOA{channel}, s);
Started synthesizing
equalizeNLS: processing frame : 1000
equalizeNLS: processing frame : 1000
equalizeNLS: processing frame : 2000
equalizeNLS: processing frame : 3000
Ended synthesizing in 5.3737 seconds.
Started synthesizing
equalizeNLS: processing frame : 1000
equalizeNLS: processing frame : 1000
equalizeNLS: processing frame : 2000
equalizeNLS: processing frame : 3000
Ended synthesizing in 5.4079 seconds.

Convolution with the source signal

% Choose 10 seconds and resample
Sr = resample(S(1:44.e3*10,:),480,441);
numOfLsp = size(s.lspLocs,1);
Y = zeros(size(Sr,1),numOfLsp);

% Resample H to 48e3 [Hz] sampling frequency for auralization
H{1} = resample(H{1},1,4);
H{2} = resample(H{2},1,4);

for channel = 1:2;
    for lsp = 1:numOfLsp
        % Convolution with Matlab's overlap-add
        Y(:,lsp) = Y(:,lsp) +  fftfilt(H{channel}(:,lsp),Sr(:,channel));
% Y contains the auralization of the spatial IRs with S

Saving the auralization to a file

Save the file to the default folder with a custom filename. Save the result as wav, as wav can handle upto 256 channels.

disp('Started Auralization');tic
savename = [ir_filename '_' audio_filename '.wav'];
if max(abs(Y(:))) > 1
    Y = Y/max(abs(Y(:)))*.9;
    disp('Sound normalized, since otherwise would have clipped')
disp(['Ended Auralization in ' num2str(toc) ' seconds.'])
disp('Started writing the auralization wav file')
disp([savename  ' on the disk.']);tic
info = audioinfo(savename);
disp('Wrote ... ');
disp(['... in ' num2str(toc) ' seconds'])
Started Auralization
Sound normalized, since otherwise would have clipped
Ended Auralization in 0.024904 seconds.
Started writing the auralization wav file
IR_living_room_paper_navy_swan_song.wav on the disk.
Wrote ... 
             Filename: 'C:\Users\tervos\Desktop\SDMtoolbox\release\IR_livi...'
    CompressionMethod: 'Uncompressed'
          NumChannels: 6
           SampleRate: 48000
         TotalSamples: 478912
             Duration: 9.9773
                Title: []
              Comment: []
               Artist: []
        BitsPerSample: 16

... in 0.10982 seconds

Playback using Matlab or other applications

% <--- EOF demoBinauralRendering.m