# How to convolve a 3D matrix along one of its dimension?

45 vues (au cours des 30 derniers jours)
Yuxin Tong le 26 Avr 2022
Commenté : Yuxin Tong le 27 Avr 2022
I have a 100by100by2000 ish matrix, say 3rd dimention is representing time where the 100*100 matrix changes. I wanted to convolve this whole matrix through a temporal gaussian filter along the 3rd dimention (the one that is 2000ish long).
With the code I know of I can only think of making a for-loop and do conv() 10000 times. (cuz I believe conv() can only operat on single dimention vectors?) However this for-loop will then be extremely long and it seems like it would take forever to run. I have attached a visual illustration of what I wanted to do.
So my question is: is there a function that is equivilent to conv() that can allow me to do this in more eifficient way? Or can I actually make conv() to run in a vectorized way?
I am aware that there is a fucntion called filter(), but I wasnt really sure what does that function do. If filter actually would work can anyone tell me what is the similarity and difference between filter and conv?
Thx a lot.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Matt J le 26 Avr 2022
If your Gaussian kernels is 1x1xN, you can just use convn as usual
kernel=reshape(gaussianProfile,1,1,[]);
output=convn(Array, kernel);
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Yuxin Tong le 27 Avr 2022
Thx! I think this worked!

Connectez-vous pour commenter.

### Plus de réponses (3)

Akira Agata le 26 Avr 2022
The function smoothdata must be applicable, like:
% Sample data
A = rand(100, 100, 2000);
% Gaussian filter window
win = 15;
% Apply smoothdata along 3rd dimension
dim = 3;
A2 = smoothdata(A, dim, 'gaussian', win);
% Let's check!
figure
plot(squeeze(A(1, 1, :)),'c')
hold on
plot(squeeze(A2(1, 1, :)),'b')
legend({'Original', 'After filtering'})
title('Signal A(1,1,:)')
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Matt J le 26 Avr 2022
You can use ffts
kernel=reshape(gaussianProfile,1,1,[]);
N=size(Array,3)+size(kernel,3)-1;
F=@(z)fft(z,N,3);
invF=@(z)ifft(z,[],3,'symmetric');
kernel=reshape(gaussianProfile,1,1,[]);
output = invF( F(kernel).*F(Array) );
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Matt J le 26 Avr 2022
Modifié(e) : Matt J le 26 Avr 2022
If you have the Image Processing Toolbox, you can use imgaussfilt3,
output = imgaussfilt3(Array,[.1,.1,sigma]);
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matched Filter and Ambiguity Function dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by