1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab code to Python

  1. Nov 27, 2016 #1
    Hello,
    1. The problem statement, all variables and given/known data

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

    2. Relevant equations
    This is the matlab code:
    Code (Matlab M):

    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
     
    3. The attempt at a solution

    Code (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: Nov 28, 2016
  2. jcsd
  3. Nov 30, 2016 #2

    jedishrfu

    Staff: Mentor

    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Matlab code to Python
  1. Matlab code (Replies: 14)

Loading...