Bonjour, j'ai un projet à réaliser, je dois avec un raspberry pi et quelques composants comme un micro de contact, un convertisseur analogique numérique... Le problème est que mon code ne fonctionne pas très bien. Le graphique est généré et s'affiche, mais je crois qu'il ne récupère pas bien les données du microphone de contact alors que le branchement est correct.
Pouvez-vous m'aider et m'indiquer ce qui ne va pas dans mon code :
Code Python : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from __future__ import division
 
import numpy as np
 
import pylab as pl
 
import random
 
import Adafruit_ADS1x15
 
# # Créez une instance ADS1015 ADC (12 bits).
adc = Adafruit_ADS1x15.ADS1015()
 
GAIN = 1
 
adc.start_adc(0, gain=GAIN)
 
t = np.array([])
 
for i in range(20):
 
 t = np.append(t, [adc.get_last_result(), ])
 
 
 
LW = 2 #Largeur de la ligne
 
AC = 0.5 #cannal alpha
 
pi = np.pi
 
 
 
def periodogramSS(inputsignal,fsamp):
 
 N = len(inputsignal)
 
 N_notnan = np.count_nonzero(~np.isnan(inputsignal))
 
 hr = fsamp/N #Résolution de fréquence
 
 #flow,fhih = -fsamp/2,(fsamp/2)+hr #spectre a deux côtés
 
 flow,fhih = 0,fsamp/2+hr #Spectre à un côté
 
 #flow,fhih = hr,fsamp/2
 
 frange = np.arange(flow,fhih,hr)
 
 fN = len(frange)
 
 Aspec = np.zeros(fN)
 
 n = 0
 
 for f in frange:
 
  Aspec[n] = np.abs(np.nansum(inputsignal*np.exp(-2j*pi*f*t)))/N_notnan
 
  n+=1
 
 Aspec *= 2 #single-sided spectrum
 
 Aspec[0] /= 2 #Composant DC divisé par 2 (i.e. halved)
 
 return (frange,Aspec)
 
 
 
#construire signal de référence:
 
f1 = 10 #Hz
 
T = 1/f1
 
fs = 10*f1
 
Ts = 1/fs
 
t = np.arange(0,20*T,Ts)
 
DC = 3.0
 
x = DC + 1.5*np.cos(2*pi*f1*t)
 
 
 
#supprimer les valeurs du signal x aléatoirement:
 
ndel = 10 #nombre d'échantillons à remplacer avec NaN
 
random.seed(0)
 
L = len(x)
 
randidx = random.sample(range(0,L),ndel)
 
for idx in randidx:
 
 x[idx] = np.nan
 
 
 
(fax,Aspectrum) = periodogramSS(x,fs)
 
 
 
fig1 = pl.figure(1,figsize=(6*3.13,4*3.13)) #plein écran
 
pl.ion()
 
 
 
pl.subplot(211)
 
pl.plot(t, x, 'b.-', lw=LW, ms=2, label='ref', alpha=AC)
 
 
 
#marquer les valeurs NaN
 
for (t_,x_) in zip(t,x):
 
 if np.isnan(x_):
 
  pl.axvline(x=t_,color='g',alpha=AC,ls='-',lw=2)
 
 
 
pl.grid()
 
pl.xlabel('Time [s]')
 
pl.ylabel('Reference signal')
 
 
 
pl.subplot(212)
 
pl.stem(fax, Aspectrum, basefmt=' ', markerfmt='r.', linefmt='r-')
 
pl.grid()
 
pl.xlabel('Frequency [Hz]')
 
pl.ylabel('Amplitude spectrum')
 
 
 
fig1name = './signal2.png'
 
print ('Saving Fig. 1 to:'), fig1name
 
fig1.savefig(fig1name)