Binning real-time data
12 ビュー (過去 30 日間)
古いコメントを表示
I need to bin real time data using Matlab. I receive data from two instruments: spectral data as (1, 1024) and timing data as (1,1). I vertically concatenate the two data and place them as columns such that each column would have the timing data as the first row and the next rows as the spectral data (1025, n; where n is the number of columns). I want to bin the spectra as columns based on the timing. My current MWE to illustrate the problem:
%%Create random data to simulate real time data
x = linspace(0.1,20,101);
y = linspace(300,1500,1024);
spectra = rand(100,1024) * 100;
%%Build test spectra
spectra_wave = [y; spectra];
time_spectra = [x; spectra_wave'];
tolerance = 0.2;
%%Binning
bins = linspace(0.2,20,40); % Bins the simulated data
binned_spectra = [];
for time_idx = 1:size(time_spectra(:,2:end),2)
timing = time_spectra(1, time_idx);
find_bin = find(bins >= timing - tolerance & ...
bins <= timing + tolerance)
binned_spectra{time_idx} = ...
vertcat(bins(1,find_bin), time_spectra(:,find_bin));
end
Basically, my current attempt is to use the find function in Matlab and use conditionals based on the tolerance as the "bin size" to bin my data. My original idea is to just vertically concatenate each spectra that belongs to the same bin as per the tolerance criteria. I realized that this method is not possible since there will be cases that each bin will have more than the other bins. My "band aid" solution is using cells as seen on my MWE. I was wondering if there are any elegant solutions that this.
0 件のコメント
回答 (1 件)
Leepakshi
2025 年 5 月 2 日
Hi,
A solution is to use MATLAB's discretize function, which automatically assigns each timing value to a bin defined by your chosen bin edges. This way, you can quickly group your spectra by these bin indices, store them in a cell array, and avoid complex loops or manual tolerance checks.
% Simulate data
x = linspace(0.1,20,101); % timing (1x101)
spectra = rand(100,1024) * 100; % spectra (100x1024)
spectra_wave = [linspace(300,1500,1024); spectra]; % (1025x100)
time_spectra = [x; spectra_wave']; % (1025x101)
% Binning using discretize
bin_edges = linspace(0.2,20,41); % 40 bins
time = time_spectra(1,:);
bin_idx = discretize(time, bin_edges);
binned_spectra = cell(1, length(bin_edges)-1);
for b = 1:length(binned_spectra)
idx = find(bin_idx == b);
if ~isempty(idx)
binned_spectra{b} = time_spectra(:, idx);
end
end
Refer to documentation below for more information:
Hope this helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Real-Time Simulation and Testing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!