B4J Question Pybridge usage ...

rbghongade

Well-Known Member
Licensed User
Longtime User
Dear friends ,
I want to pass an B4J array of doubles and compute wavelet decomposition using the python script and return the result to B4J.
Function to compute wavelet decomposition:
import pywt
import numpy as np

def compute_wavelet_decomposition(data, wavelet_name='db1'):
  
    

    Parameters:
        data (list or np.array): The input signal.
        wavelet_name (str): The type of wavelet to use (default is 'db1').
    Returns:
 
    data = np.array(data)
  
    cA, cD = pywt.dwt(data, wavelet_name)
    return cA.tolist(), cD.tolist()
I tried to use the example provided by default but was unsuccessful.
Any pointer in the right direction is highly appreciable.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Install dependencies:
pip install numpy pywavelets

B4X:
Private Sub ComputeWaveletDecomposition (Data As List) As ResumableSub
    Dim Code As String = $"
import pywt
import numpy as np
def ComputeWaveletDecomposition (Data, wavelet_name='db1'):
    data = np.array(Data)
    cA, cD = pywt.dwt(data, wavelet_name)
    return cA.tolist(), cD.tolist()
"$
    Wait For (Py.RunCode("ComputeWaveletDecomposition", Array(Data), Code).Fetch) Complete (res As PyWrapper)
    Return res.Value
End Sub

Usage:
B4X:
Wait For (ComputeWaveletDecomposition(Array(0.1, 0.2, 0.4))) Complete (cACd() As Object)
Dim ca As List = cACd(0)
Dim cd As List = cACd(1)
Log(ca)
Log(cd)
 
Upvote 0
Top