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

3 ビュー (過去 30 日間)
Bugrahan Ustundag
Bugrahan Ustundag 2022 年 8 月 30 日
編集済み: Bugrahan Ustundag 2022 年 9 月 4 日
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;

採用された回答

dpb
dpb 2022 年 8 月 30 日
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*

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by