Découvert sur Python

Fermé
GSXR38400 Messages postés 2 Date d'inscription jeudi 23 mars 2023 Statut Membre Dernière intervention 23 mars 2023 - 23 mars 2023 à 09:06
_Ritchi_ Messages postés 21215 Date d'inscription samedi 17 mars 2007 Statut Contributeur Dernière intervention 7 mai 2024 - 25 mars 2023 à 19:10

Bonjour,

Je récupère un programme en Python et je souhaiterai comment il fonctionne, merci de votre aide. Je suis un débutant en Python.

 # -*- coding: utf-8 -*-
"""
Created on Fri Jun 30 11:31:09 2017

@author:
"""

import wave, struct
import math
import binascii
import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt
print("Création d'un fichier audio au format WAV (PCM 16 bits mono 96000 Hz)")
print("Son de forme sinusoïdale sur chaque canal\n")

###############################################################################
NomFichier = '1S30_100s_21_1+11.wav'
Monson = wave.open(NomFichier,'w') # instanciation de l'objet Monson
# codé sur 1 octet amplitude = 127.5
nbCanal = 6    # mono 1
nbOctet = 2    # taille d'un échantillon : 1 octet = 8 bits
fech = 44100   # fréquence d'échantillonnage
amplitude = (2**(nbOctet*8)-1)/2

T=21
f_1S30 = 2447 #a 30°C
f_1S30 = f_1S30 * math.sqrt((273.15+T)/303.15) 



duree =100.
nb_sweep = 1
frequenceInit = f_1S30 -60
frequenceFinal = f_1S30 +20

print frequenceInit,f_1S30,frequenceFinal
###############################################################################

duree_sweep = int(duree*fech)
#nbEchantillon = nbCanal*nb_sweep*duree_sweep
nbEchantillon = nb_sweep*duree_sweep
duree_pulse=int(1)

print"Nombre d'échantillons :",nbEchantillon
print('Veuillez patienter...')
print 'nombre de mode', 1

parametres = (nbCanal,nbOctet,fech,nbEchantillon,'NONE','not compressed')# tuple
Monson.setparams(parametres)    # création de l'en-tête (44 octets)

phi0=0.
value0=0
for sweep in range(0,nb_sweep):
    for i in range(0,duree_sweep): 
        k=(frequenceFinal-frequenceInit)/float(duree)
        value = int(amplitude*math.cos(phi0+2.*math.pi*(frequenceInit*float(i)/float(fech)+0.5*k*(float(i)/float(fech))**2)))
        if (i<duree_pulse):
            value5= int(amplitude/3.)  
        else:
            value5=0
        data1 = struct.pack('<h', value)
        data2 = struct.pack('<h', value0)
        data3 = struct.pack('<h', value)
        data4 = struct.pack('<h', value0)
        data5 = struct.pack('<h', value5)
        data6 = data5
        Monson.writeframesraw(data1+data2+data3+data4+data5+data6)
        #Monson.writeframesraw(data+data)
    #        valG = wave.struct.pack('B',int(128.0 + amplitude*math.sin(2.0*math.pi*frequence*i/fech)))
    #    # canal droit
    #        valD = wave.struct.pack('B',int(128.0 + amplitude*math.sin(2.0*math.pi*frequence*i/fech)))
    #        Monson.writeframes(valG + valD) # écriture frame
    phi0= phi0+2.*math.pi*(frequenceInit*float(i+1)/float(fech)+0.5*k*(float(i+1)/float(fech))**2)

Monson.close()
print 'fichier', NomFichier, 'fait' 

  J'aimerai juste savoir comment il fonctionne à partir de la boucle

for sweep in range(0,nb_sweep): jusqu'à la fin de la boucle For. 

Merci par avance de votre aide pour la compréhension de ce programme.

Bien cordialement


Windows / Firefox 102.0


1 réponse

_Ritchi_ Messages postés 21215 Date d'inscription samedi 17 mars 2007 Statut Contributeur Dernière intervention 7 mai 2024 6 058
25 mars 2023 à 19:10

Bonjour,

La boucle for qui t'intéresse est principalement basée sur les possibilités offertes par la librairie struct dont voici les détails: https://docs.python.org/fr/3/library/struct.html
Tu y découvriras la fonction pack qui fait ceci:
"Renvoie un objet bytes contenant les valeurs v1, v2… agrégées conformément à la chaîne de format format. Les arguments doivent correspondre exactement aux valeurs requises par le format."

Ritchi

0