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
相关推荐
做萤石二次开发的哈哈4 分钟前
连锁门店、园区、交通枢纽的视频如何统一汇集?应用模板的接入与分权管理详解
人工智能·物联网·音视频·二次开发·视频监控·蓝海aiot一站式工作台·萤石云开放平台
高级程序源10 分钟前
django校园寻物通失物招领平台32261-计算机课程设计、毕业设计
vue.js·python·mysql·小程序·django·flask·课程设计
老金带你玩AI16 分钟前
全程拿着手机,我让dot准备了一场AI分享
人工智能
WeiXin_DZbishe22 分钟前
基于springboot宠物领养系统-计算机毕设 附源码【课程设计】94217
vue.js·spring boot·python·django·php·课程设计
nvd1123 分钟前
Flink 批处理驱动的湖仓 AI 财务研报流水线:从双流广播汇聚到 Agent 智能决策与原生 Iceberg 落盘全景实战
大数据·人工智能·flink
kimnoic38 分钟前
Python数据库学习心得:SQLite、MySQL、PostgreSQL优缺点
开发语言·python
snhna44 分钟前
向量数据库深度实战:从 HNSW 到 pgvector、Milvus 选型与 RAG 落地
数据库·人工智能·python·postgresql·milvus
架构技术专栏1 小时前
Agent 工具调用:从工具声明到执行反馈
前端·人工智能·后端
点云-激光雷达-Slam-三维牙齿1 小时前
100,000 Records CSV File - India Traffic Accident Analysis Dataset.7z
人工智能·数据集
TechWayfarer1 小时前
精度极限的突破:AI与IPv6如何重塑高精度IP查询
人工智能·网络协议·tcp/ip·transformer