Python Forum
Convolution "same" in Python doesn't work as Matlab
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convolution "same" in Python doesn't work as Matlab
#1
Hello, I'm trying to replicate Matlab's convolution aka conv function.

Matlab's conv works like this:

Quote:w = conv(u,v,shape) returns a subsection of the convolution, as specified by shape. For example, conv(u,v,'same') returns only the central part of the convolution, the same size as u.

I tried numpy convolve but documentation says it work differently, in fact:

numpy.convolve(a, v, mode) where:
- a(N,) array_like
- v(M,) array_like
- mode{‘full’, ‘valid’, ‘same’}

but the problem is that 'same' does NOT work as Matlab, in fact it says

Quote:Mode ‘same’ returns output of length max(M, N).

But I don't want the output to be of length max(M,N)!!! I want it to be same size as u as Matlab's.

Any idea how to do that?
Reply
#2
If M < N, then max(M, N) == N

Why should M be bigger than N?

I use the convolve function to smooth graphs.
The array M should be smaller than N.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Sep-30-2020, 12:22 PM)DeaD_EyE Wrote: If M < N, then max(M, N) == N

Why should M be bigger than N?

I use the convolve function to smooth graphs.
The array M should be smaller than N.

If M < N it for me it doesn't work

To give you some examples:

result = numpy.convolve(a, v, 'same')
case #1: a size (M) is 971, v size (N) is 601: Matlab's and Python's result is equal.

case #2: a size (M) is 451, v size (N) is 601: Matlab's and Python's result is NOT equal. The resulting array has 601 values - which is max(M,N) - but it should have 451 as in Matlab.

case #3: a size (M) is 59, v size (N) is 61: Matlab's and Python's result is NOT equal. The resulting array has 61 values - which is max(M,N) - but it should have 59 as in Matlab.
Reply
#4
You can trim the second array to the same size of the first array.
The other options for mode aren't helpful.

How is matlab doing this?
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Sep-30-2020, 02:46 PM)DeaD_EyE Wrote: You can trim the second array to the same size of the first array.
The other options for mode aren't helpful.

How is matlab doing this?

What do you mean by "how is matlab doing this" ?

As in my previous post, Matlab's result of a 'same' convolution always has the same size of the first parameter array.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem of converting Matlab code to python DongyanZ 2 1,376 Feb-03-2023, 01:04 PM
Last Post: jefsummers
  Numpy doesn't work in Spyder 4.1.3 player1681 1 2,103 Jun-02-2020, 11:26 AM
Last Post: jefsummers
  Python equivalent of Matlab code kwokmaster 5 6,995 Apr-03-2020, 12:02 AM
Last Post: scidam
  scipy interp2d doesn't work player1681 4 4,401 Feb-05-2020, 09:50 AM
Last Post: player1681
  Is there similar function to lsqnonlin (in matlab) in python? Jay_Nerella 1 5,879 Nov-11-2019, 08:40 AM
Last Post: feli_x
  Matlab to Python Ariane 6 6,998 Jun-14-2018, 12:08 PM
Last Post: Ariane
  How to use .m matlab file in python ? sameer 5 15,290 May-10-2018, 09:39 AM
Last Post: wavic
  cx_Freeze doesn't seem to work owenwalker65 7 16,745 Dec-07-2017, 02:23 AM
Last Post: issac_n
  Importing matlab cell array (.mat) into a python list scanato 0 8,600 Nov-15-2017, 11:04 AM
Last Post: scanato
  Numpy or Scipy Variance Convolution TimWebPhoenix 0 3,317 Jul-11-2017, 12:41 PM
Last Post: TimWebPhoenix

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020