当前位置: 首页 > news >正文

TensorFlow2 study notes[1]

文章目录

  • audio
  • references

audio

  1. it is simple to install tensorflow as follows:
pip install torch torchvision torchaudio
  1. tf.audio.decode_wav
    you can condense a 16-bit PCM WAV file into a float tensor.
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.
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
http://www.lryc.cn/news/585793.html

相关文章:

  • 【每日算法】专题八_分治_归并排序
  • The Practice of Programming
  • 【2025/07/11】GitHub 今日热门项目
  • 2025十大免费销售管理软件推荐
  • AIGC(生成式AI)试用 33 -- 用AI学AI名词
  • [spring6: @EnableLoadTimeWeaving]-使用案例
  • 人脸图像生成(DCGAN)
  • Linux入门篇学习——Linux 编写第一个自己的命令,make 工具和 makefile 文件
  • C++编程基础
  • 大模型在卵巢癌预测及诊疗方案制定中的应用研究
  • Linux驱动基本概念(内核态、用户态、模块、加载、卸载、设备注册、字符设备)
  • Allegro 17.4操作记录
  • 【理念●体系】从零打造 Windows + WSL + Docker + Anaconda + PyCharm 的 AI 全链路开发体系
  • 数据库系统的基础知识(三)
  • uniapp---入门、基本配置了解
  • spring-ai RAG(Retrieval-Augmented Generation)
  • ESP32_启动日志分析
  • 力扣 hot100 Day41
  • RLHF:人类反馈强化学习 | 对齐AI与人类价值观的核心引擎
  • Linux711 Mysql
  • openpilot:为您的汽车插上智能驾驶的翅膀
  • 创意总监的动态视觉秘诀:用AE动态遮罩AI,轻松实现“人景分离”
  • 【每日刷题】加一
  • Java 中的锁分类
  • 【牛客刷题】吃糖果----糖果甜度问题(贪心策略详解)
  • 小车循迹功能的实现(第六天)
  • UML 与 SysML 图表对比全解析:软件工程 vs 系统工程建模语言
  • 持有对象-泛型和类型安全的容器
  • 线程通信V
  • 【Linux】系统引导修复