6 from audio_common_msgs.msg
import AudioData
7 from jsk_recognition_msgs.msg
import Spectrum
16 super(AudioToSpectrum, self).
__init__()
22 mic_sampling_rate = rospy.get_param(
'~mic_sampling_rate', 16000)
24 fft_sampling_period = rospy.get_param(
'~fft_sampling_period', 0.3)
26 bitdepth = rospy.get_param(
'~bitdepth', 16)
30 rospy.logerr(
"'~bitdepth' {} is unsupported.".format(bitdepth))
37 window_function = np.arange(
40 2 * np.pi * window_function)
41 high_cut_freq = rospy.get_param(
'~high_cut_freq', 800)
42 if high_cut_freq > mic_sampling_rate / 2:
43 rospy.logerr(
'Set high_cut_freq lower than {} Hz'.format(
44 mic_sampling_rate / 2))
45 low_cut_freq = rospy.get_param(
'~low_cut_freq', 1)
46 self.
freq = np.fft.fftfreq(
49 (low_cut_freq <= self.
freq) & (self.
freq <= high_cut_freq),
59 '~spectrum', Spectrum, queue_size=1)
61 '~spectrum_filtered', Spectrum, queue_size=1)
67 audio_buffer = np.frombuffer(data, dtype=self.
dtype)
69 audio_buffer = audio_buffer[0::self.
n_channel]
81 amplitude = np.log(np.abs(amplitude))
82 spectrum_msg = Spectrum()
83 spectrum_msg.header.stamp = rospy.Time.now()
84 spectrum_msg.amplitude = amplitude
85 spectrum_msg.frequency = self.
freq 86 self.pub_spectrum.publish(spectrum_msg)
87 spectrum_msg.amplitude = amplitude[self.
cutoff_mask]
89 self.pub_spectrum_filtered.publish(spectrum_msg)
92 if __name__ ==
'__main__':
93 rospy.init_node(
'audio_to_spectrum')
def timer_cb(self, timer)