how to heat map encode a smoothed line of sparse time-series data
Show older comments
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
More Answers (1)
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:
- interp1: https://www.mathworks.com/help/matlab/ref/double.interp1.html
- colormap: https://www.mathworks.com/help/matlab/ref/colormap.html
Glad to help!
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

