Clear Filters
Clear Filters

Reading int16 from a file and writing as int8 to another file

4 views (last 30 days)
I want to read an I/Q sampled binary file and write it to another file.
I have a binary file consisting of signals stored as complex 16- bit samples at a rate of 25 Msps.
I want to write this file to another file with 8-bit samples and I want to execute that in a loop since sizes of the files are enormous.
But when I try to write to another file, I could not adjust its starting point.
I share my code below, where do I get wrong?
clear all;
clc;
tic;
duration = 20; % seconds to be read
start_time =0;
sample_rate=25000000;
point_to_begin=0;
for c=0:duration
fid=fopen('/home/read.bin','r'); % open the file
fseek(fid, point_to_begin+sample_rate*c, 'bof');% position the start
s_int16=fread(fid,sample_rate,'int16')';% read in Is and Qs
fclose(fid);
s_int8=typecast(s_int16,'int8'); %typecast int16 to int8
fileID = fopen('/home/write.bin','w');
fwrite(fileID,s_int8,'int8',point_to_begin+sample_rate*c);
fclose(fileID);
end
toc;

Accepted Answer

dpb
dpb on 30 Aug 2022
Don't open/close the files inside the loop -- just read/write them sequentially...
...
fidi=fopen('/home/read.bin','r'); % open the file
fido = fopen('/home/write.bin','w');
for c=0:duration
s_int16=fread(fidi,sample_rate,'int16')';
s_int8=typecast(s_int16,'int8');
fwrite(fido,s_int8,'int8');
end
fclose('all')
clear fid*

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!