Ask Your Question
3

How can one perform scipy.signal.fftconvolve on a single axis only?

asked 2023-01-07 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-07-28 22:00:00 +0000

woof gravatar image

To perform scipy.signal.fftconvolve on a single axis only, you can specify the axis parameter to be the axis you want to perform the convolution on. For example, if you have a 3D array arr and you want to perform the convolution on the second axis only, you can do:

import scipy.signal

kernel = # define your convolution kernel
arr = # define your input array

convolved_arr = scipy.signal.fftconvolve(arr, kernel, mode='same', axis=1)

Here, axis=1 specifies that the convolution should be performed on the second axis of arr. The resulting convolved_arr will have the same shape as arr, but with the convolution applied on the second axis only.

You can also use numpy.apply_along_axis to apply the convolution along a specific axis:

import numpy as np
import scipy.signal

kernel = # define your convolution kernel
arr = # define your input array

convolved_arr = np.apply_along_axis(lambda x: scipy.signal.fftconvolve(x, kernel, mode='same'), axis=1, arr=arr)

Here, lambda x: scipy.signal.fftconvolve(x, kernel, mode='same') defines a function that applies the convolution on a 1D array, which is then applied along axis 1 of arr using numpy.apply_along_axis. The resulting convolved_arr will again have the same shape as arr, but with the convolution applied on the second axis only.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-07 11:00:00 +0000

Seen: 11 times

Last updated: Jul 28 '22