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
相关推荐
玫幽倩1 分钟前
2026黄河流域公安院校-电子物证单项赛(程序逆向分析+服务器取证)
运维·服务器·python·电子取证·逆向·程序分析·服务器取证
恒拓高科WorkPlus3 分钟前
从“可选项”到“必选项”——私有化部署即时通讯如何重塑企业协作新底座
大数据·网络·人工智能
北斗落凡尘4 分钟前
LangGraph 入门实战(4)
python·langchain
Hello_Damon_Nikola5 分钟前
扰流筋从原理到应用
人工智能·单片机·嵌入式硬件
某林2126 分钟前
从“仿真能跑”到“真机能走”:一套轮腿机械狗强化学习系统的工程化实践
人工智能·stm32·嵌入式硬件·架构·机器人·机械狗
爱知菜15 分钟前
Transformer vs Diffusion:为什么扩散模型更擅长捕捉细节?
人工智能·深度学习·transformer·扩散模型
xp_xht12319 分钟前
Day1 概率机器学习 信息论
人工智能·机器学习·信息论
liwulin050624 分钟前
【PYTHON】使用Selenium + ChromeDriver以及XPATH语法
开发语言·python·selenium
copyer_xyf26 分钟前
Agentic RAG 实战:PostgreSQL + LangGraph 一条链路
python·postgresql·agent
chuan.bai32 分钟前
Java RAG 实战(第 3 篇):从交互式聊天到多轮上下文
java·人工智能·macos·ai