spectrum_plot.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 from __future__ import division
5 
6 import cv_bridge
7 from jsk_topic_tools import ConnectionBasedTransport
8 import matplotlib
9 from audio_to_spectrogram import check_matplotlib_version; check_matplotlib_version()
10 matplotlib.use('Agg')
11 import matplotlib.pyplot as plt
12 import numpy as np
13 import rospy
14 import sensor_msgs.msg
15 
16 from jsk_recognition_msgs.msg import Spectrum
17 
18 from audio_to_spectrogram import convert_matplotlib_to_img
19 
20 
21 class SpectrumPlot(ConnectionBasedTransport):
22 
23  def __init__(self):
24  super(SpectrumPlot, self).__init__()
25  self.min_amp = rospy.get_param('~min_amp', 0.0)
26  self.max_amp = rospy.get_param('~max_amp', 20.0)
27  self.queue_size = rospy.get_param('~queue_size', 1)
28  max_rate = rospy.get_param('~max_rate', -1)
29  if max_rate > 0:
30  self.min_period = 1.0 / max_rate
31  else:
32  self.min_period = -1
33  self.prev_pub_tm = None
34  # Set matplotlib config
35  self.fig = plt.figure(figsize=(8, 5))
36  self.fig.subplots_adjust(left=0.1, right=0.95, top=0.90, bottom=0.1,
37  wspace=0.2, hspace=0.6)
38  self.ax = self.fig.add_subplot(1, 1, 1)
39  self.ax.grid(True)
40  # self.fig.suptitle('Spectrum plot', size=12)
41  self.ax.set_title('Spectrum plot', fontsize=12)
42  # Use self.ax.set_title() instead of
43  # self.fig.suptitle() to use self.fig.tight_layout()
44  # preventing characters from being cut off
45  # cf. https://tm23forest.com/contents/matplotlib-tightlayout-with-figure-suptitle
46  # https://matplotlib.org/2.2.4/tutorials/intermediate/tight_layout_guide.html
47  self.ax.set_xlabel('Frequency [Hz]', fontsize=12)
48  self.ax.set_ylabel('Amplitude', fontsize=12)
49  self.line, = self.ax.plot([0, 0], label='Amplitude of Spectrum')
50  # ROS publisher and subscriber
51  self.pub_img = self.advertise(
52  '~output/viz', sensor_msgs.msg.Image, queue_size=1)
53 
54  def subscribe(self):
55  self.sub_spectrum = rospy.Subscriber(
56  '~spectrum', Spectrum, self._cb, queue_size=self.queue_size)
57 
58  def unsubscribe(self):
59  self.sub_spectrum.unregister()
60 
61  def _cb(self, msg):
62  # Keep max_rate
63  if self.min_period > 0:
64  curr_tm = rospy.Time.now().to_sec()
65  if self.prev_pub_tm is not None:
66  elapsed_tm = curr_tm - self.prev_pub_tm
67  if elapsed_tm < 0:
68  # Time moved backwards (e.g., rosbag play --loop)
69  pass
70  elif elapsed_tm < self.min_period:
71  return
72  self.prev_pub_tm = curr_tm
73  # Plot spectrum
74  self.amp = np.array(msg.amplitude)
75  self.freq = np.array(msg.frequency)
76  self.line.set_data(self.freq, self.amp)
77  self.ax.set_xlim((self.freq.min(), self.freq.max()))
78  self.ax.set_ylim((self.min_amp, self.max_amp))
79  self.ax.legend(loc='upper right')
80  self.fig.tight_layout()
81  if self.pub_img.get_num_connections() > 0:
82  bridge = cv_bridge.CvBridge()
83  img = convert_matplotlib_to_img(self.fig)
84  img_msg = bridge.cv2_to_imgmsg(img, encoding='rgb8')
85  img_msg.header = msg.header
86  self.pub_img.publish(img_msg)
87 
88 
89 if __name__ == '__main__':
90  rospy.init_node('spectrum_plot')
91  SpectrumPlot()
92  rospy.spin()
spectrum_plot.SpectrumPlot.min_amp
min_amp
Definition: spectrum_plot.py:25
spectrum_plot.SpectrumPlot.pub_img
pub_img
Definition: spectrum_plot.py:51
spectrum_plot.SpectrumPlot.min_period
min_period
Definition: spectrum_plot.py:30
audio_to_spectrogram.convert_matplotlib.convert_matplotlib_to_img
def convert_matplotlib_to_img(fig)
Definition: convert_matplotlib.py:13
spectrum_plot.SpectrumPlot.__init__
def __init__(self)
Definition: spectrum_plot.py:23
spectrum_plot.SpectrumPlot.amp
amp
Definition: spectrum_plot.py:74
spectrum_plot.SpectrumPlot.sub_spectrum
sub_spectrum
Definition: spectrum_plot.py:55
spectrum_plot.SpectrumPlot._cb
def _cb(self, msg)
Definition: spectrum_plot.py:61
spectrum_plot.SpectrumPlot.max_amp
max_amp
Definition: spectrum_plot.py:26
audio_to_spectrogram.compat.check_matplotlib_version
def check_matplotlib_version(python_version=os.getenv('ROS_PYTHON_VERSION'))
Definition: compat.py:8
spectrum_plot.SpectrumPlot.prev_pub_tm
prev_pub_tm
Definition: spectrum_plot.py:33
spectrum_plot.SpectrumPlot.unsubscribe
def unsubscribe(self)
Definition: spectrum_plot.py:58
spectrum_plot.SpectrumPlot.freq
freq
Definition: spectrum_plot.py:75
spectrum_plot.SpectrumPlot.subscribe
def subscribe(self)
Definition: spectrum_plot.py:54
spectrum_plot.SpectrumPlot.fig
fig
Definition: spectrum_plot.py:35
spectrum_plot.SpectrumPlot.queue_size
queue_size
Definition: spectrum_plot.py:27
spectrum_plot.SpectrumPlot.ax
ax
Definition: spectrum_plot.py:38
spectrum_plot.SpectrumPlot
Definition: spectrum_plot.py:21


audio_to_spectrogram
Author(s):
autogenerated on Sat Feb 22 2025 04:03:22