How to plot .bin file?

I make some binary file .bin files by connectiong an active GPS antenna to USRP 2900, now can any one help me how to plot those files using MATLAB to check that satellites are acquired or not on a given gain?

12 Comments

dpb
dpb on 16 May 2023
Need to know how the files are structured to have any klew how to read them -- what's the word length/numeric format/endianness/????
Rizwan
Rizwan on 16 May 2023
the data is recorded by using following parameters;
Carrier frequency = 1575.42MHz
I/Q rate = 4MHz
Gain 0-30dB
Write Buffer I/Q Samples = 10000
dpb
dpb on 16 May 2023
Edited: dpb on 16 May 2023
Tells us nothing about the file format and how it was saved/written...
The USRP 2900 has a 12 bit analog to digital convertor.
There are three common methods of returning data in such cases:
  • packed binary, 3 bytes holds the information for two samples; these numbers may need to be scaled to appropriate units;
  • unpacked binary, usually with the 12 bits as the first 12 bits of a 16 bit integer, with the bottom filled in with 0; these numbers may need to be scaled to appropriate units;
  • single precision numbers, having already been scaled to appropriate units
There is also the possibility of 12 bits as the bottom 12 bits of a 16 bit integer, but that is less common.
You need to know how the data was read, and whether it needs to be unpacked, and whether it needs to be scaled.
Since i am not an expert to know that know these common mthods, i just use above parameters and using USRP 2900 with the help of LABView I record the signal in the .bin format i am sending you the LabView VI along with the matlab code that i am using for plot that data.......
close all;
clear variables;
% Open the binary file
fid = fopen('test01.bin', 'rb'); % 'rb' for reading binary data
% Read the binary data (assuming it contains received signal strength values)
signal_strength = fread(fid, 'float32'); % Assuming float32 for signal strength values
% Close the file
fclose(fid);
% Generate a histogram
figure;
histogram(signal_strength, 'BinWidth', 1);
xlabel('Signal Strength');
ylabel('Count');
title('Satellite Signal Strength Histogram');
% Find peaks in the histogram
[counts, edges] = histcounts(signal_strength);
[peaks, locs] = findpeaks(counts, edges(1:end-1));
% Plot vertical lines for each identified peak
hold on;
for i = 1:numel(locs)
line([locs(i) locs(i)], [0 peaks(i)], 'Color', 'r', 'LineWidth', 2);
end
hold off;
Please also check the LABView VI that is attached with this comment.VI_01 is for record and VI_02 is for playback...
dpb
dpb on 17 May 2023
That's all up to what/how LabView writes the data, then; not MATLAB. That's in the doc's for it, not a MATLAB question, per se.
fread() will read bytes and interpret them how it is told to do so; what the bytes are and how they are to be interpreted is up to the application that wrote them; you'll have to ferret that out of the LabView doc's.
I used it some 30+ years ago, but that's ancient history, now...
I would suggest replacing that for loop with a call to stem
Rizwan
Rizwan on 18 May 2023
i tried it with stem but it makes my laptop so slow that even I feel like i am using computer in 90s
>> x=randn(10000,1);tic;stem(x);toc
Elapsed time is 1.003551 seconds.
>> tic;stem(x);toc
Elapsed time is 0.040224 seconds.
>> tic;stem(x);toc
Elapsed time is 0.028222 seconds.
>> tic;stem(x);toc
Elapsed time is 0.023083 seconds.
>> tic;stem(x);toc
Elapsed time is 0.072088 seconds.
>> tic;stem(x);toc
Elapsed time is 0.014547 seconds.
Would have to see the code; sounds like you still left it in the loop???
stem is decent performer although as shows, there's a cache miss the first time to load the function itself...
Replace
for i = 1:numel(locs)
line([locs(i) locs(i)], [0 peaks(i)], 'Color', 'r', 'LineWidth', 2);
end
with
stem(locs, 'r', 'LineWidth', 2);
with no loop.
stem(locs, 'r', 'LineWidth', 2);
should be
stem(locs,peaks, 'r', 'LineWidth', 2);
@dpb is right, the peaks are needed as well.

Sign in to comment.

Answers (1)

I can help you with plotting the data from your .bin files using MATLAB. To get started, you'll need to read the binary file and extract the relevant data. Since you haven't provided the specific format of your .bin files, I'll assume that the data is stored as a series of numerical values representing satellite acquisition information.
Here's an example MATLAB code that demonstrates how you can read the binary file, extract the data, and plot it:
% Specify the path to your .bin file
file_path = 'path/to/your/file.bin';
% Open the binary file for reading
file_id = fopen(file_path, 'rb');
% Read the binary data into a numerical array
data = fread(file_id, 'double');
% Close the file
fclose(file_id);
% Specify the gain threshold for satellite acquisition
gain_threshold = 10; % Adjust this value as needed
% Plot the data
figure;
plot(data, 'b');
hold on;
plot(find(data >= gain_threshold), data(data >= gain_threshold), 'ro');
hold off;
% Set plot labels and title
xlabel('Sample Index');
ylabel('Gain');
title('Satellite Acquisition');
% Display a legend
legend('Gain', 'Acquired Satellites');
% Adjust plot settings if needed
grid on;
Make sure to replace 'path/to/your/file.bin' with the actual file path of your .bin file. Also, adjust the gain_threshold value according to the gain level at which you consider a satellite to be acquired.
This code reads the binary file using fread, stores the data in the data array, and then plots the entire data as a blue line. It also marks the points where the gain exceeds the specified threshold with red circles.
Feel free to customize the code further based on your specific requirements or the structure of your binary file.

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 16 May 2023

Answered:

on 22 May 2023

Community Treasure Hunt

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

Start Hunting!