how to heat map encode a smoothed line of sparse time-series data

9 views (last 30 days)
Hello.
I have data capturing the motion of a hand while clicking a mouse. The y-values are sparse points of computed instantaneous frequency of the clicks and the x-values are the time locations of the clicks. I wish to do two things:
(1) plot a smooth line between the sparse data points
(2) color encode as a heat map the related speed on the line
It is not at all clear to me how to do (2) and what would be the best smoothing to do given the sparcity while still capturing as best the theoretical changes if they were continuous.
Attached is some data to load along with related code to better understand what I am aiming to visualize.
Any help as also is greatly appreciate. Cheers!
%%
load 'testData.mat'
time = Data.time;
freq = Data.freq;
speeds = Data.speeds;
figure;
plot(time, freq)
ylabel('freq (Hz)');
xlabel('seconds');

Accepted Answer

William Rose
William Rose on 25 Sep 2024
Edited: William Rose on 25 Sep 2024
[Edit: Add label to colorbar.]
The script below plots the raw data and smoothly interpolated data, colored by speed. You can read the help for interp1 and try a different interpolation method. Method 'spline' is very smooth, but it can have significant undershoot and overshoot. Method 'makima' is less smooth than 'spline', and has less undershoot and overshoot. Method 'pchip' has no undershoot or overshoot, but it can be less smooth looking than 'makima'. I use method 'makima' below.
load 'testData.mat'
time = Data.time;
freq = Data.freq;
speeds = Data.speeds;
% Compute interpolated, evenly sampled signal.
N=1000; % number of data points in interpolated, evenly sampled signal
t2=linspace(time(1),time(end),N);
freq2=interp1(time,freq,t2,'makima');
spd2=interp1(time,speeds,t2,'makima');
% Plot results
figure;
plot(time,freq,'k*','MarkerSize',10); % plot original data
hold on
scatter(t2,freq2,20,spd2,'filled') % add interpolated data
ylabel('freq (Hz)'); xlabel('seconds');
cb=colorbar(); % add colorbar
ylabel(cb,'Speed') % add colorbar label
The plot colors the interpolated points by speed.
  4 Comments
William Rose
William Rose on 25 Sep 2024
If you expect to see an inverse relationship between speed and frequency, then plot the two:
load 'testData.mat'
time = Data.time;
freq = Data.freq;
speeds = Data.speeds;
Compute values for the best-fit straight line:
coefficients = polyfit(freq, speeds, 1);
xFit = [min(freq), max(freq)];
yFit = polyval(coefficients , xFit);
Plot results
figure;
plot(freq,speeds,'b*',xFit,yFit,'-b'); % plot data and fitted line
xlabel('Freq (Hz)'); ylabel('Speed')
legend('Data','Best Fit Line'); grid on
This is not the most convincing inverse relationship.

Sign in to comment.

More Answers (1)

Epsilon
Epsilon on 25 Sep 2024
Edited: Epsilon on 25 Sep 2024
Hi hxen,
For plotting a smooth line between the sparse points, 1-D interpolation can be used with the spline method.
Eample code:
% Interpolate for smoothing
smooth_time = linspace(min(time), max(time), 1000);
smooth_freq = interp1(time, freq, smooth_time, 'spline');
To color encode the related speed on the line use a colormap, different options can be used as per the choice.
Example code:
% Normalize speeds for color mapping
norm_speeds = (speeds - min(speeds)) / (max(speeds) - min(speeds));
figure;
hold on;
cmap = autumn(1000); % Choose different colormap options!
% Plot with color encoding
for i = 1:length(smooth_time)-1
[~, idx] = min(abs(time - smooth_time(i)));
color = cmap(round(norm_speeds(idx) * 999) + 1, :);
plot(smooth_time(i:i+1), smooth_freq(i:i+1), 'Color', color, 'LineWidth', 2);
end
% Add colorbar
colormap(autumn); % Choose different colormap options!
colorbar;
caxis([min(speeds) max(speeds)]);
Please refer to the documentation on 1-D interpolation and colormap for further help:
Glad to help!

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!