TensorFlow2 study notes[1]

文章目录

audio

  1. it is simple to install tensorflow as follows:
bash 复制代码
pip install torch torchvision torchaudio
  1. tf.audio.decode_wav
    you can condense a 16-bit PCM WAV file into a float tensor.
python 复制代码
import tensorflow as tf

# Read the WAV file as a byte string
contents = tf.io.read_file("audio_file.wav")

# Decode the WAV file
audio, sample_rate = tf.audio.decode_wav(contents, desired_channels=1)

print("Audio shape:", audio.shape)  # [samples, channels]
print("Sample rate:", sample_rate.numpy())  # e.g., 44100 (Hz)
  1. tf.audio.encode_wav achive the data Encoding of audio with the WAV file format.
python 复制代码
import tensorflow as tf
import matplotlib.pyplot as plt

# Generate a simple sine wave (1 second, 440 Hz, mono)
sample_rate = 44100  # 采样率 (44.1 kHz)
frequency = 440.0    # 频率 (440 Hz, A4 音)
duration = 2.0       # 时长 (2 秒)

# 生成时间轴 (0 到 1 秒)
t = tf.linspace(0.0, duration, int(sample_rate * duration))

# 生成 440 Hz 正弦波
audio = tf.sin(2 * 3.141592 * frequency * t)

# 绘制前 5 毫秒的波形(约 2 个周期)
plt.plot(t[:200], audio[:200])  # 44100 Hz / 440 Hz ≈ 100 点/周期
plt.title("440 Hz 正弦波 (采样率 44.1 kHz)")
plt.xlabel("时间 (秒)")
plt.ylabel("振幅")
plt.show()
# Encode to WAV format
audio = tf.expand_dims(audio, axis=-1) 
wav_data = tf.audio.encode_wav(audio, sample_rate)

# Save to a file
tf.io.write_file("test.wav", wav_data)

references

  1. https://tensorflow.google.cn/api_docs/python/tf/all_symbols
  2. deepseek
相关推荐
七月shi人2 小时前
【AI编程工具IDE/CLI/插件专栏】-国外IDE与Cursor能力对比
ide·人工智能·ai编程·代码助手
橙 子_3 小时前
基于 Amazon Nova Sonic 和 MCP 构建语音交互 Agent
python
2zcode5 小时前
基于Matlab的深度学习智能行人检测与统计系统
人工智能·深度学习·目标跟踪
宇寒风暖5 小时前
Flask 框架全面详解
笔记·后端·python·学习·flask·知识
哪 吒5 小时前
【2025C卷】华为OD机试九日集训第3期 - 按算法分类,由易到难,提升编程能力和解题技巧
python·算法·华为od·华为od机试·2025c卷
weixin_464078075 小时前
机器学习sklearn:过滤
人工智能·机器学习·sklearn
weixin_464078075 小时前
机器学习sklearn:降维
人工智能·机器学习·sklearn
数据与人工智能律师5 小时前
智能合约漏洞导致的损失,法律责任应如何分配
大数据·网络·人工智能·算法·区块链
张艾拉 Fun AI Everyday5 小时前
小宿科技:AI Agent 的卖铲人
人工智能·aigc·创业创新·ai-native
zhongqu_3dnest5 小时前
三维火灾调查重建:科技赋能,探寻真相
人工智能