After plotting the graph according to the conditions, you must use the if statement to plot or make the warning message disappear. However, once the warning message is plotted, it does not disappear. Below is the corresponding code.
import os
from vcd.reader import TokenKind, tokenize
from vcd.writer import VCDWriter
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def read_vcd(file_path):
signals = {}
current_time = 0
with open(file_path, 'r') as f:
for token in tokenize(f):
if token.kind == TokenKind.TIMESTAMP:
current_time = token.value
elif token.kind == TokenKind.CHANGE_SCALAR:
if token.reference not in signals:
signals[token.reference] = []
signals[token.refe
rence].append((current_time, token.value))
# Fill missing data points
for ref in signals:
filled_signals = []
last_value = '0'
for time in range(1, current_time + 1):
if signals[ref] and signals[ref][0][0] == time:
last_value = signals[ref].pop(0)[1]
filled_signals.append((time, last_value))
signals[ref] = filled_signals
for ref, data in signals.items():
print(f"Signal: {ref}")
for time, value in data[:10]: # 첫 10개의 데이터만 출력
print(f"Time: {time}, Value: {value}")
return signals
def write_vcd(signals, output_file):
timescale = '1 us'
with open(output_file, 'w') as f:
writer = VCDWriter(f, timescale=timescale, date='today')
refs = {}
for ref in signals:
refs[ref] = writer.register_var('scope', ref, 'integer', size=8)
for time in range(1, len(next(iter(signals.values()))) + 1):
writer.change(time, {refs[ref]: signals[ref][time - 1][1] for ref in signals})
writer.close()
def plot_vcd(signals):
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(10, 8))
x_data = []
y_data1, y_data2, y_data3 = [], [], []
lines = [ax1.plot([], [])[0], ax2.plot([], [])[0], ax3.plot([], [])[0]]
def init():
for line in lines:
line.set_data([], [])
return lines
def animate(i):
x_data.append(i)
y_data1.append(int(signals['sensor1'][i][1]))
y_data2.append(int(signals['sensor2'][i][1]))
y_data3.append(int(signals['sensor3'][i][1]))
for j, (y_data, line, ax) in enumerate(zip([y_data1, y_data2, y_data3], lines, [ax1, ax2, ax3])):
line.set_data(x_data, y_data)
ax.set_xlim(0, len(x_data))
ax.set_ylim(0, max(14, max(y_data) + 1))
if y_data[-1] >= 13:
line.set_color('red')
ax.set_facecolor('lightcoral')
ax.set_title(f'Sensor {j+1}: Warning - High Value', color='red')
else:
line.set_color('blue')
ax.set_facecolor('white')
ax.set_title(f'Sensor {j+1}', color='black')
return lines
ani = animation.FuncAnimation(fig, animate, init_func=init, frames=len(next(iter(signals.values()))), interval=100, blit=True)
plt.show()
input_vcd = 'C:/intelFPGA/18.1/graph_plot.vcd'
signals = read_vcd(input_vcd)
output_vcd = 'C:/intelFPGA/18.1/graph_plot_filled.vcd'
write_vcd(signals, output_vcd)
`
signals_filled = read_vcd(output_vcd)
plot_vcd(signals_filled)
enter image description here
I tried using if instead of if else in the conditional statement and modified the process of loading the vcd file, but there was no significant difference.
Alberto7948 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.