Main Content

sofawrite

Write SOFA files

Since R2023b

    Description

    sofawrite(filename,s) writes the data in the object s to a SOFA file specified by filename.

    example

    Examples

    collapse all

    Create a SOFA template object following the SimpleFreeFieldHRIR convention.

    s = sofaconvention("SimpleFreeFieldHRIR");

    Generate mock impulse response data to store in the file. Set the number of measurements M to two and set the impulse response length N to 1024. The number of receivers R is two, corresponding to two ears. Use the fir1 function to create arbitrary FIR filters for the impulse response measurements.

    M = 2;
    N = 1024;
    R = 2;
    
    IR = zeros(M,R,N);
    IR(1,:,:) = [fir1(N-1,0.1);fir1(N-1,.2)];
    IR(2,:,:) = [fir1(N-1,0.15);fir1(N-1,.25)];

    Store the measurements in the SOFA object.

    s.Numerator = IR;

    Set the ListenerView so the positive x-axis of the local listener coordinate system points up with a 45 degree elevation. Set the ReceiverPosition so the ears are 18 cm apart on the x-axis.

    s.ListenerViewType = "spherical";
    s.ListenerView = [0 45 0];
    s.ReceiverPosition = [0 0 0; 0 0.18 0];

    Modify the Title and Comment metadata properties.

    s.Title = "My SOFA File";
    s.Comment = "This SOFA file contains mock FIR data.";

    Save the data to a SOFA file.

    sofawrite("myFile.sofa",s);

    Load a SOFA file.

    s = sofaread("ReferenceHRTF.sofa");

    Inspect the DateModified property to see when the file was last modified.

    s.DateModified
    ans = 
    "2023-04-28 13:37:09"
    

    Modify the Title and History properties of the SOFA object.

    s.Title = "Modified title";
    s.History = sprintf("%s\nModified title.", s.History);

    Save the modified object to a new SOFA file.

    sofawrite("myFile.sofa",s);

    Create a SOFA template object following the SimpleFreeFieldHRTF convention to store measurement data as complex frequency responses.

    s = sofaconvention("SimpleFreeFieldHRTF");

    Load HRTF data containing impulse response measurements. Initialize the FrequencyResponse property to store 100 measurements in 512-point frequency responses.

    load 'ReferenceHRTF.mat' hrtfData sourcePosition sampleRate
    M = 100;
    N = 512;
    R = 2; % 2 ears
    
    s.FrequencyResponse = complex(zeros(M,R,N));

    For 100 of the measurements from the HRTF data, use freqz to get the complex frequency responses from the FIR filters and store them in the SOFA object.

    for i = 1:M
        ear1FIR = squeeze(hrtfData(:,i,1));
        ear2FIR = squeeze(hrtfData(:,i,2));
        
        [h1,f] = freqz(ear1FIR,1,N,sampleRate);
        s.FrequencyResponse(i,1,:) = h1;
        h2 = freqz(ear2FIR,1,N,sampleRate);
        s.FrequencyResponse(i,2,:) = h2;
    end
    
    s.FrequencyVector = f;

    Set the SourcePosition property to store the positional data, in spherical coordinates, corresponding to the selected measurements.

    s.SourcePositionType = "spherical";
    s.SourcePosition = sourcePosition(1:M,1:3);

    Write the data to a SOFA file.

    sofawrite("freqResponseData.sofa",s);

    Create a SOFA template object following the SimpleFreeFieldHRSOS convention to store measurement data as second-order section (SOS) filters.

    s = sofaconvention("SimpleFreeFieldHRSOS");

    Generate mock SOS data to store in the file. Use designVarSlopeFilter to create an SOS filter to use for both receivers in a single measurement.

    M = 1;
    N = 4;
    R = 2;
    s.Numerator = zeros(M,R,N,3);
    s.Denominator = zeros(M,R,N,3);
    
    [num,den] = designVarSlopeFilter(24,.2,Orientation="row");
    s.Numerator(1,1,:,:) = num;
    s.Numerator(1,2,:,:) = num;
    s.Denominator(1,1,:,:) = den;
    s.Denominator(1,2,:,:) = den;

    Save the data to a SOFA file.

    sofawrite("mockSOSData.sofa",s);

    Input Arguments

    collapse all

    Name of the SOFA file, specified as a string scalar or character vector.

    Data Types: char | string

    More About

    collapse all

    SOFA Files

    Spatially oriented format for acoustics (SOFA) is a file format for storing spatially oriented acoustic data like head-related transfer functions (HRTF) and binaural or spatial room impulse responses. The AES69 standard [3] defines the SOFA file format.

    References

    [1] Majdak, P., Zotter, F. Brinkmann, F., De Muynke, J., Mihocic, M., and Noisternig, M., “Spatially Oriented Format for Acoustics 2.1: Introduction and Recent Advances.” Journal of the Audio Engineering Society 70, no. 7/8 (July 25, 2022): 565–84. https://doi.org/10.17743/jaes.2022.0026.

    [2] Majdak, P., Carpentier, T., Nicol, R., Roginska, A., Suzuki, Y., Watanabe, K., Wierstorf, H., et al., “Spatially Oriented Format for Acoustics: A Data Exchange Format Representing Head-Related Transfer Functions.” In Proceedings of the 134th Convention of the Audio Engineering Society (AES), Paper 8880. Roma, Italy, 2013.

    [3] AES69-2022. "AES standard for file exchange - Spatial acoustic data file format." Standard of the Audio Engineering Society. https://www.aes.org/publications/standards/search.cfm?docID=99

    Version History

    Introduced in R2023b