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
相关推荐
AI营销干货站几秒前
原圈科技AI市场分析白皮书:决胜2026,重塑市场预测与决策
人工智能
董厂长1 分钟前
Agent 意图库 和 知识图谱
人工智能·llm·agent·意图识别
海天一色y14 分钟前
基于CNN实现Mnist手写数字识别
人工智能·深度学习·计算机视觉
爬山算法18 分钟前
Hibernate(44)Hibernate中的fetch join是什么?
前端·python·hibernate
说私域19 分钟前
基于AI智能名片链动2+1模式预约服务商城小程序的数据管理与系统集成研究
大数据·人工智能·小程序
AC赳赳老秦24 分钟前
技术文档合著:DeepSeek辅助多人协作文档的风格统一与内容补全
android·大数据·人工智能·微服务·golang·自动化·deepseek
咚咚王者25 分钟前
人工智能之核心基础 机器学习 第十四章 半监督与自监督学习总结归纳
人工智能·学习·机器学习
一代明君Kevin学长29 分钟前
记录一个上手即用的Spring全局返回值&异常处理框架
java·网络·python·spring
教游泳的程序员33 分钟前
【面试问题精选】java开发工程师
python·面试·职场和发展
风栖柳白杨33 分钟前
【语音识别】SenseVoice非流式改流式
人工智能·语音识别