%% Write and Read Fixed-Point Data
% The dsp.BinaryFileWriter and dsp.BinaryFileReader System objects do not support writing and reading fixed-point data. As a workaround, you can write the stored integer portion of the fi data, read the data, and use this value to reconstruct the fi data.
%% Write the Fixed-Point Data
% Create an fi object to represent 100 signed random numbers with a word length of 14 and a fraction length of 12. Write the stored integer portion of the fi object to the data file myFile.dat. The built-in data type is int16, which can be computed using class(storeIntData).

data = randn(100,1);
fiDataWriter = fi(data,1,14,12);
storeIntData = storedInteger(fiDataWriter);

writer = dsp.BinaryFileWriter('myFile.dat');
writer(storeIntData);

%
% Release the writer so that the reader can access the data.

release(writer);

%
%% Read the Fixed-Point Data
% Specify the reader to read the stored integer data as int16 data with 100 samples per data frame. The real-world value of the fixed-point number can be represented using 2^{(-fractionLength)(storedInteger)}. If you know the signedness, word length, and fraction length of the fixed-point data, you can reconstruct the fi data using fi(realValue,signedness,wordLength,fractionLength). In this example, the data is signed with a word length of 14 and a fraction length of 12.

reader = dsp.BinaryFileReader('Filename','myFile.dat','SamplesPerFrame',100,...
    'DataType','int16');
data = reader();
fractionLength = 12;
wordLength = 14;
realValue = 2^(-fractionLength)*double(data);

fiDataReader = fi(realValue,1,wordLength,fractionLength);

%
% Verify that the writer data is the same as the reader data.

isequal(fiDataWriter,fiDataReader)

% Copyright 2012 The MathWorks, Inc.