spectrum_to_spectrogram.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import division
4 
5 import cv2
6 from cv_bridge import CvBridge
7 import numpy as np
8 import rospy
9 from distutils.version import LooseVersion
10 import pkg_resources
11 
12 from jsk_recognition_msgs.msg import Spectrum
13 from sensor_msgs.msg import Image
14 
15 
16 # This node publish spectrogram (sensor_msgs/Image)
17 # from spectrum (jsk_recognition_msgs/Spectrum)
18 
19 class SpectrumToSpectrogram(object):
20 
21  def __init__(self):
22  super(SpectrumToSpectrogram, self).__init__()
23  # Set spectrogram shape
24  self.image_height = rospy.get_param('~image_height', 300)
25  self.image_width = rospy.get_param('~image_width', 300)
26  # Get spectrum length
27  spectrum_msg = rospy.wait_for_message('~spectrum', Spectrum)
28  spectrum_len = len(spectrum_msg.amplitude)
29  # Buffer for spectrum topic
30  self.spectrogram = np.zeros((0, spectrum_len), dtype=np.float32)
31  self.spectrum_stamp = []
32  # Period[s] to store audio data to create one spectrogram topic
33  self.spectrogram_period = rospy.get_param('~spectrogram_period', 5)
34  # ROS subscriber and publisher
35  rospy.Subscriber(
36  '~spectrum', Spectrum, self.audio_cb)
37  self.pub_spectrogram = rospy.Publisher(
38  '~spectrogram', Image, queue_size=1)
39  publish_rate = rospy.get_param(
40  '~publish_rate', float(self.image_width / self.spectrogram_period))
41 
42  timer_kwargs = dict(
43  period=rospy.Duration(1.0 / publish_rate),
44  callback=self.timer_cb,
45  oneshot=False,
46  )
47  if (LooseVersion(pkg_resources.get_distribution('rospy').version) >=
48  LooseVersion('1.12.0')) and rospy.get_param('/use_sim_time', None):
49  # on >=kinetic, it raises ROSTimeMovedBackwardsException
50  # when we use rosbag play --loop.
51  timer_kwargs['reset'] = True
52  self.timer = rospy.Timer(**timer_kwargs)
53  self.bridge = CvBridge()
54 
55  def audio_cb(self, msg):
56  # Add spectrum msg to buffer
57  spectrum = np.array(msg.amplitude, dtype=np.float32)
58  self.spectrogram = np.concatenate(
59  [self.spectrogram, spectrum[None]])
60  self.spectrum_stamp.append(msg.header.stamp)
61  # Extract spectrogram of last (self.spectrogram_period) seconds
62  time_now = rospy.Time.now()
63  for i, stamp in enumerate(self.spectrum_stamp):
64  if (time_now - stamp).to_sec() < self.spectrogram_period:
65  self.spectrogram = self.spectrogram[i:]
66  self.spectrum_stamp = self.spectrum_stamp[i:]
67  break
68 
69  def timer_cb(self, timer):
70  if self.spectrogram.shape[0] == 0:
71  return
72  # Reshape spectrogram
73  spectrogram = self.spectrogram.transpose(1, 0)[::-1, :]
74  spectrogram = cv2.resize(
75  spectrogram, (self.image_width, self.image_height))
76  # Publish spectrogram
77  spectrogram_msg = self.bridge.cv2_to_imgmsg(spectrogram, '32FC1')
78  spectrogram_msg.header.stamp = self.spectrum_stamp[-1]
79  self.pub_spectrogram.publish(spectrogram_msg)
80 
81 
82 if __name__ == '__main__':
83  rospy.init_node('audio_to_spectrogram')
85  rospy.spin()
spectrum_to_spectrogram.SpectrumToSpectrogram.audio_cb
def audio_cb(self, msg)
Definition: spectrum_to_spectrogram.py:55
spectrum_to_spectrogram.SpectrumToSpectrogram.spectrum_stamp
spectrum_stamp
Definition: spectrum_to_spectrogram.py:31
spectrum_to_spectrogram.SpectrumToSpectrogram.image_height
image_height
Definition: spectrum_to_spectrogram.py:24
spectrum_to_spectrogram.SpectrumToSpectrogram.timer
timer
Definition: spectrum_to_spectrogram.py:52
spectrum_to_spectrogram.SpectrumToSpectrogram.spectrogram
spectrogram
Definition: spectrum_to_spectrogram.py:30
spectrum_to_spectrogram.SpectrumToSpectrogram.__init__
def __init__(self)
Definition: spectrum_to_spectrogram.py:21
spectrum_to_spectrogram.SpectrumToSpectrogram
Definition: spectrum_to_spectrogram.py:19
spectrum_to_spectrogram.SpectrumToSpectrogram.timer_cb
def timer_cb(self, timer)
Definition: spectrum_to_spectrogram.py:69
spectrum_to_spectrogram.SpectrumToSpectrogram.pub_spectrogram
pub_spectrogram
Definition: spectrum_to_spectrogram.py:37
spectrum_to_spectrogram.SpectrumToSpectrogram.image_width
image_width
Definition: spectrum_to_spectrogram.py:25
spectrum_to_spectrogram.SpectrumToSpectrogram.spectrogram_period
spectrogram_period
Definition: spectrum_to_spectrogram.py:33
spectrum_to_spectrogram.SpectrumToSpectrogram.bridge
bridge
Definition: spectrum_to_spectrogram.py:53


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