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
相关推荐
hundaxxx1 小时前
自演化大语言模型的技术背景
人工智能
数智顾问1 小时前
【73页PPT】美的简单高效的管理逻辑(附下载方式)
大数据·人工智能·产品运营
love530love2 小时前
【保姆级教程】阿里 Wan2.1-T2V-14B 模型本地部署全流程:从环境配置到视频生成(附避坑指南)
人工智能·windows·python·开源·大模型·github·音视频
木头左2 小时前
结合机器学习的Backtrader跨市场交易策略研究
人工智能·机器学习·kotlin
Coovally AI模型快速验证2 小时前
3D目标跟踪重磅突破!TrackAny3D实现「类别无关」统一建模,多项SOTA达成!
人工智能·yolo·机器学习·3d·目标跟踪·无人机·cocos2d
研梦非凡2 小时前
CVPR 2025|基于粗略边界框监督的3D实例分割
人工智能·计算机网络·计算机视觉·3d
MiaoChuAI2 小时前
秒出PPT vs 豆包AI PPT:实测哪款更好用?
人工智能·powerpoint
He1955012 小时前
Go初级之十:错误处理与程序健壮性
开发语言·python·golang
fsnine2 小时前
深度学习——残差神经网路
人工智能·深度学习
和鲸社区3 小时前
《斯坦福CS336》作业1开源,从0手搓大模型|代码复现+免环境配置
人工智能·python·深度学习·计算机视觉·语言模型·自然语言处理·nlp