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

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.xdnf.cn/news/15123.html

相关文章:

  • NW710NW713美光固态闪存NW719NW720
  • 【每日刷题】回文数
  • c语言中的数组IV
  • 奇哥面试:RabbitMQ工作模式深度剖析与Spring整合MQ
  • Datawhale AI夏令营:基于带货视频评论的用户洞察挑战赛上分全攻略
  • 数据库系统的基础知识(三)
  • 【时时三省】(C语言基础)通过指针引用数组元素
  • Redis 分片集群
  • C++中的智能指针(1):unique_ptr
  • 《汇编语言:基于X86处理器》第7章 整数运算(2)
  • 星云穿越与超光速飞行特效的前端实现原理与实践
  • 上位机知识篇---Linux软硬链接
  • 用 ELK+Filebeat 提高50%问题排查效率,这套方案实测有效!
  • cnpm exec v.s. npx
  • Shader面试题100道之(81-100)
  • python之set详谈
  • LeetCode经典题解:128、最长连续序列
  • TCP服务器与客户端三种方法实现
  • Linux权限的概念
  • SM712.TCT Semtech TVS二极管——电子设备的终极电路守护
  • DNS(Domain Name System,域名系统)
  • 计算机毕业设计ssm晋中大学城校园论坛 SSM大学城学生社区互动管理平台 JavaWeb高校校园信息交流与服务系统
  • java底层的native和沙箱安全机制
  • 系统思考:多元胜过能力
  • 鸿蒙 Secure Boot 全流程解析:从 BootROM 到内核签名验证的实战指南
  • 2025 年值得尝试的 6 大内容管理系统 (CMS)
  • 【实用IP查询工具】IP数据云-IP地址查询离线库使用方案
  • 【操作系统】Linux 中的 exec 命令
  • RK3566/RK3568 Android11 CAN开发(内核配置+测试验证+安卓app开发)
  • STM32F103之存储/启动流程