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
相关推荐
lpfasd1231 天前
2026年第17周GitHub趋势周报:AI代理工程化与端侧智能加速落地
人工智能·github
nervermore9901 天前
2.人工智能学习-环境搭建
人工智能
Flying pigs~~1 天前
LoRA 面试完全指南:低秩分解原理 + Transformer 应用
人工智能·深度学习·lora·大模型·微调·transformer
大橙子打游戏1 天前
薅满火山引擎每天数百万免费 Tokens:我写了一个自动轮换代理
人工智能
lpfasd1231 天前
2026年第17周科技社区趋势周报
人工智能·科技
IT_陈寒1 天前
SpringBoot配置加载顺序把我坑惨了
前端·人工智能·后端
集和诚JHCTECH1 天前
BRAV-7120加持,让有毒有害气体无处遁形
大数据·人工智能·嵌入式硬件
iwhitney1 天前
【次方量化】3分钟搞懂什么是量化策略
python
高洁011 天前
大模型部署资源不足?轻量化部署解决方案
python·深度学习·机器学习·数据挖掘·transformer
机械X人1 天前
Encoder-Decoder PLM
人工智能·深度学习