3
$\begingroup$

Say I have 5 measurements of a signal as a column vector: x. I can smooth the signal by multiplying it with a smoothing matrix:

$\ \mathbf{xs=S*x} $

For instance I might use [1 2 1] as a smoothing kernel in which case I get:

S =
     2     1     0     0     0
     1     2     1     0     0
     0     1     2     1     0
     0     0     1     2     1
     0     0     0     1     2

So far I have been using the function sparse() to build the matrix diagonal by diagonal, but this quickly becomes tedious.

How can I quickly create a mxm smoothing matrix from a 1xn convolution kernel in Matlab?

$\endgroup$
2
  • $\begingroup$ Why don't you use filter instead? Something like filter([1 2 1],1,x) $\endgroup$
    – Juancho
    Jul 5 '12 at 16:24
  • $\begingroup$ Convolve seems a good candidate, too... $\endgroup$ Jul 5 '12 at 18:24
5
$\begingroup$

You could do the following-

kernel = [1 2 1];
s = conv2(eye(numMeasurements), kernel, 'same')
$\endgroup$
6
  • $\begingroup$ Thanks Jim. That works fine, but is there not some matrix operation: S=operation(eye(n,n),kernel) ? $\endgroup$
    – Andy
    Jul 5 '12 at 16:46
  • $\begingroup$ Not that I'm aware of. $\endgroup$
    – Jim Clay
    Jul 5 '12 at 16:50
  • $\begingroup$ @Andy Changed the solution to make it much more compact and more like you want, I think. $\endgroup$
    – Jim Clay
    Jul 5 '12 at 20:23
  • $\begingroup$ Thanks Jim! Something like that was exactly what I was looking for. $\endgroup$
    – Andy
    Jul 6 '12 at 10:53
  • $\begingroup$ Sorry I can't comment yet, but just in case you didn't know, you could define a function in another .m file and use it like your suggested "S=operation(eye(n,n),kernel)" if that's what you really want. $\endgroup$ Jul 6 '12 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.