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
相关推荐
阿泽·黑核几秒前
Easy Vibe Coding 学习心得(六):RAG 入门——让 AI 拥有企业级知识库
人工智能·vibe coding·easy vibe
光之后裔3 分钟前
人工智能对计算机领域冲击思考
人工智能
学习指针路上的小学渣6 分钟前
Selenium笔记
python·selenium
AI人工智能+8 分钟前
一种融合大模型微调与高精度OCR的智能文档抽取系统,实现对合同文本中关键要素的高精度语义理解与结构化抽取
人工智能·语言模型·ocr·文档抽取
程序员水自流10 分钟前
【AI大模型第13集】Transformer底层架构原理详细介绍(核心组件拆解分析)
java·人工智能·架构·llm·transformer
code_pgf11 分钟前
openclaw配置高德导航、京东商品搜索、QQ 音乐播放控制
人工智能·gateway·边缘计算
SunnyDays101111 分钟前
使用 Python 在 Excel 中应用数据验证:详细指南
python·excel·数据验证
IT观测12 分钟前
品牌在AI中的影响力如何评估?2026年AI营销工具实战选型指南
大数据·人工智能
ai_xiaogui13 分钟前
PanelAI前端全面升级!私有化部署AI面板控制台+生态市场一键管理详解
前端·人工智能·comfyui一键部署·生态市场算力共享·ai面板控制台·panelai私有化部署·大模型前端管理
Jelena1577958579215 分钟前
1688.item_get_app接口:包装尺寸重量信息深度解析
开发语言·前端·python