Issue
sosfilt
from the scipy=1.9.3
library is giving me a strange ValueError when inputting a numpy=1.23.4
array.
MWE:
import numpy as np
from scipy.signal import sosfilt
fs=48000
rng = np.random.default_rng()
signal = rng.normal(size=(fs))
b_0k = [1.01589602025559, 0.958943219304445, 0.961371976333197,
2.22580350360974, 0.471735128494163, 0.115267139824401,
0.988029297230954, 1.95223768730136]
b_1k = [-1.92529887777608, -1.80608801184949, -1.76363215433825,
-1.43465048479216, -0.366091796830044, 0.0, -1.91243380293387,
0.162319983017519]
b_2k = [0.922118060364679, 0.876438777856084, 0.821787991845146,
-0.498204282194628, 0.244144703885020, -0.115267139824401,
0.926131550180785, -0.667994113035186]
a_0k = np.ones(len(b_0k))
a_1k = [-1.92529887777608, -1.80608801184949, -1.76363215433825,
-1.43465048479216, -0.366091796830044, -1.79600256669201,
-1.91243380293387, 0.162319983017519]
a_2k = [0.938014080620272, 0.835381997160530, 0.783159968178343,
0.727599221415107, -0.284120167620817, 0.805837815618546,
0.914160847411739, 0.284243574266175]
sos = np.array([b_0k, b_1k, b_2k, a_0k, a_1k, a_2k]).T
signalFiltered = sosfilt(sos, signal, axis=0)
gives me
signalFiltered = sosfilt(sos, signal, axis=0) Traceback (most recent call last):
Cell In[102], line 1 signalFiltered = sosfilt(sos, signal, axis=0)
File C:\ProgramData\Miniconda3\envs\devenv\lib\site-packages\scipy\signal_signaltools.py:4247 in sosfilt _sosfilt(sos, x, zi)
File _sosfilt.pyx:81 in scipy.signal._sosfilt._sosfilt
File stringsource:660 in View.MemoryView.memoryview_cwrapper
File stringsource:350 in View.MemoryView.memoryview.cinit
ValueError: ndarray is not C-contiguous
However
signal.flags
shows
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
I have also tried this on other signals imported from wav files, with the same error. Having looked around, there does not seem to be any known issue or other examples of this error using this function (although there are some machine learning package bugs discussed elsewhere)
What is the problem with Scipy?
Solution
It's the transpose that's giving problems, making it F contiguous
. Add a .copy()
.
Look at the array flags, before and after transpose and copy.
transpose
works by reversing the strides and shape. It's a view
, a cheap way of doing the job, but messes with the continuity.
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.