# Most Efficient Way to Smooth Wavelet Spectral Matrix over Time and Frequency

2 views (last 30 days)
Anas Khan on 23 Oct 2022
Answered: William Rose on 24 Oct 2022
I want to smooth a 4 dimensional matrix (channel i x channel j x frequency x time) containing wavelet coefficients across time and frequency for each channel ij pair.
Dimenions of S:
size(S)
ans =
3 3 67 4501
My solution:
% Smoothing over time
k = 50;
for chani = 1:size(S,1)
for chanj = 1:size(S,2)
for freqi = 1:size(S,3)
temporalsmoothedS(chani,chanj,freqi,:) = movmean(squeeze(S(chani,chanj,freqi,:)),k);
end
end
end
clear k chani chanj freqi
% Smoothing over frequency
k = 3;
for chani = 1:size(S,1)
for chanj = 1:size(S,2)
for timei = 1:size(S,4)
doublesmoothedS(chani,chanj,:,timei) = movmean(squeeze(S(chani,chanj,:,timei)),k);
end
end
end
clear k chani chanj timei temporalsmoothedS
Is there a way to do this better? Perhaps do time and frequency at the same time? would conv2 work? Do I absolutely need all the loops? Thanks.

William Rose on 24 Oct 2022
@Anas Khan, I think your approach is good. It has the virtues of being readable and easy to follow. You do not need
clear k chani chanj freqi
since k, chani, chanj will all be assigned new values in a line or two, and it is not necessary to remove freqi from memory.
You also don't need
clear k chani chanj timei temporalsmoothedS
for similar reasons. And in the case of temporalsmoothedS, you have computed it and then erased it without ever using it for anything - like plotting, saving to a file, etc.

### Categories

Find more on Orthogonal and Biorthogonal Filter Banks in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!