2020-12-30 12:35:06 +00:00
|
|
|
% This function opens a binary file using the ibyte format, reads and
|
|
|
|
% quantizes the signal using the most significant nbits, and stores the
|
|
|
|
% quantized signal into an output file. The output file also uses the
|
2020-04-01 18:22:46 +00:00
|
|
|
% ibyte format.
|
|
|
|
%
|
|
|
|
% Usage: quantize_signal (infile, outfile, nbits)
|
|
|
|
%
|
|
|
|
% Inputs:
|
|
|
|
% infile - Input file name
|
|
|
|
% outfile - Output file name
|
|
|
|
% nbits - number of quantization bits
|
|
|
|
%
|
2020-12-30 12:35:06 +00:00
|
|
|
|
2020-04-01 18:22:46 +00:00
|
|
|
% -------------------------------------------------------------------------
|
|
|
|
%
|
2020-12-30 12:35:06 +00:00
|
|
|
% GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2020-04-01 18:22:46 +00:00
|
|
|
% This file is part of GNSS-SDR.
|
|
|
|
%
|
2020-12-30 12:35:06 +00:00
|
|
|
% SPDX-FileCopyrightText: Marc Majoral, 2020. mmajoral(at)cttc.es
|
2020-04-01 18:22:46 +00:00
|
|
|
% SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
%
|
|
|
|
% -------------------------------------------------------------------------
|
|
|
|
%
|
2020-12-30 12:35:06 +00:00
|
|
|
|
2020-04-01 18:22:46 +00:00
|
|
|
function quantize_signal (infile, outfile, nbits)
|
|
|
|
|
|
|
|
%% usage: quantize_signal (infile, outfile, nbits)
|
|
|
|
%%
|
|
|
|
%% open input signal file infile, quantize the signal using the most
|
|
|
|
%% significant nbits and write the quantized signal to outfile
|
|
|
|
%%
|
|
|
|
|
|
|
|
fileID = fopen(infile, 'rb');
|
|
|
|
fileID2 = fopen(outfile, 'wb');
|
|
|
|
|
|
|
|
% initialize loop
|
|
|
|
BlockSize = 20000000; % processing block size
|
|
|
|
NumBitsPerSample = 8; % num. bits per sample ibyte format.
|
|
|
|
NumBitsSHift = NumBitsPerSample - nbits;
|
|
|
|
DivVal = 2^NumBitsSHift;
|
|
|
|
Lim2sCompl = 2^(NumBitsPerSample - 1) - 1;
|
|
|
|
Base2sCompl = 2^NumBitsPerSample;
|
|
|
|
do = true;
|
|
|
|
data_bytes = fread(fileID, BlockSize);
|
|
|
|
|
|
|
|
while do
|
2020-12-30 12:35:06 +00:00
|
|
|
|
|
|
|
% 2's complement
|
2020-04-01 18:22:46 +00:00
|
|
|
for k=1:length(data_bytes)
|
|
|
|
if (data_bytes(k) > Lim2sCompl)
|
|
|
|
data_bytes(k) = -Base2sCompl + data_bytes(k);
|
|
|
|
end
|
2020-12-30 12:35:06 +00:00
|
|
|
end
|
|
|
|
|
2020-04-01 18:22:46 +00:00
|
|
|
% take the nbits most significant bits
|
|
|
|
data_bytes = floor(data_bytes/DivVal);
|
2020-12-30 12:35:06 +00:00
|
|
|
|
2020-04-01 18:22:46 +00:00
|
|
|
% quantization correction
|
|
|
|
data_bytes = data_bytes*2 + 1;
|
|
|
|
|
|
|
|
% 2's complement
|
|
|
|
for k=1:length(data_bytes)
|
|
|
|
if (data_bytes(k) < 0)
|
|
|
|
data_bytes(k) = Base2sCompl + data_bytes(k);
|
|
|
|
end
|
2020-12-30 12:35:06 +00:00
|
|
|
end
|
|
|
|
|
2020-04-01 18:22:46 +00:00
|
|
|
% write result
|
|
|
|
fwrite(fileID2, data_bytes);
|
2020-12-30 12:35:06 +00:00
|
|
|
|
2020-04-01 18:22:46 +00:00
|
|
|
if (size(data_bytes) < BlockSize)
|
|
|
|
do = false;
|
|
|
|
else
|
|
|
|
data_bytes = fread(fileID,BlockSize);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
% close files
|
|
|
|
fclose(fileID);
|
2020-12-30 12:35:06 +00:00
|
|
|
fclose(fileID2);
|