Filter löschen
Filter löschen

How to read a binary file and then classify it?

2 Ansichten (letzte 30 Tage)
mostafa
mostafa am 8 Okt. 2023
Kommentiert: Walter Roberson am 29 Okt. 2023
I have a text file in " dat " format that is the output of an audio device and needs to be read in binary and then classified on it to be able to meaningfully calculate and obtain their SNR. Can someone guide me how to categorize them? (I have attached a sample file)
  3 Kommentare
Walter Roberson
Walter Roberson am 8 Okt. 2023
That file 1.dat is not a text file. It is a binary file in an unknown format.
There is no standard ".dat" file format: ".dat" is an extension used by many different software packages for very different purposes.
There is a possibility that the .dat file is a hyperspectral data file produced by ENVI, but such a file would be accompanied by a .hdr file... and it seems unlikely given that we are told that this is from an audio device and ENVI is seldom used for audio.
I made some tests trying to figure out if the data was binary integers. If you examine the data as a series of bytes (uint8) then most of the bytes are either 0 or 255, and there is a relatively even distribution of the other values bytes (except that 1 was about 4 times as common as the others). Looking at the statistical distribution of the values using other potential representation (such as single precision) I could not justify any other potential representation (though I cannot rule out that it might be some ultrasound as 16 bit unsigned integers)
So we have to ask what the data format is; and we have to ask "classify as what" ?
mostafa
mostafa am 29 Okt. 2023
Hello
I apologize for my late reply.
Actually, I want to read this file in binary form and categorize them. The method I used is as follows: first you open the file and it is read as binary, then they are divided into two categories. Then I square the first and second categories separately and then add them together.
But this method doesn't display peaks well for me. Their code is as follows:
file_id=fopen(FileName,'rb');
data = fread(file_id, 10000, 'int32');
sinw = [];
cosw = [];
for j = 1:2:length(data)
sinw =[sinw, data(j)];
cosw = [cosw, data(j+1)];
end
sinw=sinw';
cosw=cosw';
Corr=cosw.^2+sinw.^2;
Corrmax=max(Corr);
dc1=find(ismember(Corr,Corrmax));
% "find station of corrmax in corr"
if dc1<1
dc1=2;
end
meancorr=mean(Corr);
Corr=Corr./meancorr;
nn=Corr(dc1);
Corrr=mean(nn);
peaksd1=max(Corr);
SNR=10*log10(peaksd1);
Considering that this code does not give me the desired answer, I am looking for a new category to draw the peak better.
By the way, I'm still skeptical about "fread" second and third arguments. Because I don't know exactly what "Size" and "Precision" to choose for fread

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 29 Okt. 2023
Bearbeitet: Walter Roberson am 29 Okt. 2023
This assumes that the data really is int32, which I am not convinced of.
FileName = '1.dat';
file_id=fopen(FileName,'r');
data = fread(file_id, 10000, 'int32');
sinw = [];
cosw = [];
sinw = double(data(1:2:end));
cosw = double(data(2:2:end));
Corr=cosw.^2+sinw.^2;
Corrmax = max(Corr);
dc1 = find(Corr == Corrmax);
% "find station of corrmax in corr"
meancorr=mean(Corr);
Corr=Corr./meancorr;
nn=Corr(dc1);
Corrr=mean(nn);
%remember the mean could be negative in theory, so what used to be max
%might be min now
peaksd1=max(Corr);
SNR=10*log10(peaksd1);
  1 Kommentar
Walter Roberson
Walter Roberson am 29 Okt. 2023
To classify them:
[cluster, means] = kmeans(Corr, 2);
scatter(cluster, Corr)
but you will not find the result to be enlightening.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by