Python to Matlab Conversion for fscanf & DWT

In summary, the author finds it difficult to transform the fscanf function and to apply the discrete wavelet transform order 2 from matlab to python.
  • #1
Simon_Azerty
1
0
Hello,
1. Homework Statement

I find diffuculties to transform fscanf and to apply the discrete wavelet transform order 2 from matla to python.

Homework Equations


This is the MATLAB code:
Matlab:
D_P=fopen('distance_profil.txt','r');
i=0;
while(feof(D_P)==0)% test for end of file
    i=i+1;
    sign=fscanf(D_P,'%f , ');
    classe=fgetl(D_P);
      %wavelet transform
       [caH(i,:),cdH(i,:)] = dwt(sign,'db2');     
% Segmentation 
   loc=[];
        [pks,locs] = findpeaks(abs(cdH(i,:)),'threshold',3);
        loc=[loc,locs];
        mod=abs(cdH(i,:));
   figure
   titre = ['distance profil : ' classe];
    
        subplot(2,1,1); plot(sign); title(titre);
        hold on, plot(2*locs,sign(2*locs),'dr')
        subplot(2,1,2); plot(abs(cdH(i,:))); title('Module  Details coef. for db2');
        hold on, plot(locs,mod(locs),'dr') 
end

The Attempt at a Solution



Python:
import pywt
import numpy as np
from scipy.signal import find_peaks_cwt
import matplotlib.pyplot as plt

D_P= open ("distance_profil.txt","r")
i=0
while True:
    line = D_P.readline().strip()
    if line == '':

        def ReadFile():
            sign = []
            with open('textfiledata.txt', 'rt') as myFile:
                for line in myFile:
                    sign.append(map(int, line.split(',')))
            return sign

        classe = D_P.readline().rstrip()
        cA= np.array(sign)
        cD= np.array(sign)
    
        i+=1

        array[cA(i,:), cD(i,:)] = pywt.dwt([sign, 'db2'])

        loc=[]
        [pks,locs] = find_peaks_cwt(abs(cdH(i,)),'threshold',3);
        loc=[loc,locs];
        mod=abs(cdH(i,:));
      
        plt.figure()
        plt.subplot(2,2,1)
        plt.plot(sign,2*locs,sign(2*locs),'ro')
        plt.title('distance profil : ' , classe);
      
        plt.subplot(2,2,2)
        plt.plot(abs(cdH(i,:)),locs,mod(locs),'ro')
        plt.title("Module des Details coef. for db2")
        plt.show()
        break
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Fscanf simply reads in a float value and a comma and then the loop repeats the action

You could develop a similar function that reads a float at a time and when the line is exhausted read in a new line and when the file is exhausted exit the function with an error code.

Here's some that does something similar:

http://sciprogtips.blogspot.com/2010/01/fscanf-with-python.html
 

FAQ: Python to Matlab Conversion for fscanf & DWT

How do I convert a Python file to Matlab for fscanf?

To convert a Python file to Matlab for fscanf, you can use the load function in Matlab. This function can read data from a text file in various formats, including Python's read function's output. Simply specify the file name and format in the load function's parameters, and the data will be loaded into Matlab.

Can Matlab perform Discrete Wavelet Transform (DWT) like Python?

Yes, Matlab has a built-in function called dwt that can perform Discrete Wavelet Transform. This function takes in the signal and the wavelet type as parameters, and returns the wavelet coefficients and the scaling coefficients.

How do I convert a Python file to Matlab for DWT?

The process of converting a Python file to Matlab for DWT is similar to that of converting for fscanf. You can use the load function to read in the data from the Python file, and then use the dwt function to perform the transform.

Are there any differences between Python's fscanf and Matlab's fscanf?

Yes, there are a few differences between Python's fscanf and Matlab's fscanf. One difference is that Matlab's fscanf requires the format string to be specified using the % symbol, while Python's fscanf does not. Additionally, Matlab's fscanf can only read in numeric values, while Python's fscanf can also read in strings.

Can I convert my entire Python code to Matlab?

No, you cannot directly convert your entire Python code to Matlab. While both languages have some similarities, they also have significant differences in syntax and functionality. It is best to manually translate your code from Python to Matlab to ensure it runs correctly.

Similar threads

Replies
3
Views
2K
Replies
4
Views
4K
Replies
1
Views
1K
Replies
4
Views
1K
Back
Top