tf.signal.frame
Stay organized with collections
Save and categorize content based on your preferences.
Expands signal
's axis
dimension into frames of frame_length
.
tf.signal.frame(
signal,
frame_length,
frame_step,
pad_end=False,
pad_value=0,
axis=-1,
name=None
)
Used in the notebooks
Slides a window of size frame_length
over signal
's axis
dimension
with a stride of frame_step
, replacing the axis
dimension with
[frames, frame_length]
frames.
If pad_end
is True, window positions that are past the end of the axis
dimension are padded with pad_value
until the window moves fully past the
end of the dimension. Otherwise, only window positions that fully overlap the
axis
dimension are produced.
For example:
# A batch size 3 tensor of 9152 audio samples.
audio = tf.random.normal([3, 9152])
# Compute overlapping frames of length 512 with a step of 180 (frames overlap
# by 332 samples). By default, only 49 frames are generated since a frame
# with start position j*180 for j > 48 would overhang the end.
frames = tf.signal.frame(audio, 512, 180)
frames.shape.assert_is_compatible_with([3, 49, 512])
# When pad_end is enabled, the final two frames are kept (padded with zeros).
frames = tf.signal.frame(audio, 512, 180, pad_end=True)
frames.shape.assert_is_compatible_with([3, 51, 512])
If the dimension along axis
is N, and pad_end=False
, the number of frames
can be computed by:
num_frames = 1 + (N - frame_size) // frame_step
If pad_end=True
, the number of frames can be computed by:
num_frames = -(-N // frame_step) # ceiling division
Args |
signal
|
A [..., samples, ...] Tensor . The rank and dimensions
may be unknown. Rank must be at least 1.
|
frame_length
|
The frame length in samples. An integer or scalar Tensor .
|
frame_step
|
The frame hop size in samples. An integer or scalar Tensor .
|
pad_end
|
Whether to pad the end of signal with pad_value .
|
pad_value
|
An optional scalar Tensor to use where the input signal
does not exist when pad_end is True.
|
axis
|
A scalar integer Tensor indicating the axis to frame. Defaults to
the last axis. Supports negative values for indexing from the end.
|
name
|
An optional name for the operation.
|
Returns |
A Tensor of frames with shape [..., num_frames, frame_length, ...] .
|
Raises |
ValueError
|
If frame_length , frame_step , pad_value , or axis are not
scalar.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.signal.frame\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/signal/shape_ops.py#L55-L231) |\n\nExpands `signal`'s `axis` dimension into frames of `frame_length`.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.signal.frame`](https://www.tensorflow.org/api_docs/python/tf/signal/frame)\n\n\u003cbr /\u003e\n\n tf.signal.frame(\n signal,\n frame_length,\n frame_step,\n pad_end=False,\n pad_value=0,\n axis=-1,\n name=None\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Transfer Learning for the Audio Domain with TensorFlow Lite Model Maker](https://www.tensorflow.org/lite/models/modify/model_maker/audio_classification) |\n\nSlides a window of size `frame_length` over `signal`'s `axis` dimension\nwith a stride of `frame_step`, replacing the `axis` dimension with\n`[frames, frame_length]` frames.\n\nIf `pad_end` is True, window positions that are past the end of the `axis`\ndimension are padded with `pad_value` until the window moves fully past the\nend of the dimension. Otherwise, only window positions that fully overlap the\n`axis` dimension are produced.\n\n#### For example:\n\n # A batch size 3 tensor of 9152 audio samples.\n audio = tf.random.normal([3, 9152])\n\n # Compute overlapping frames of length 512 with a step of 180 (frames overlap\n # by 332 samples). By default, only 49 frames are generated since a frame\n # with start position j*180 for j \u003e 48 would overhang the end.\n frames = tf.signal.frame(audio, 512, 180)\n frames.shape.assert_is_compatible_with([3, 49, 512])\n\n # When pad_end is enabled, the final two frames are kept (padded with zeros).\n frames = tf.signal.frame(audio, 512, 180, pad_end=True)\n frames.shape.assert_is_compatible_with([3, 51, 512])\n\nIf the dimension along `axis` is N, and `pad_end=False`, the number of frames\ncan be computed by: \n\n num_frames = 1 + (N - frame_size) // frame_step\n\nIf `pad_end=True`, the number of frames can be computed by: \n\n num_frames = -(-N // frame_step) # ceiling division\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------|----------------------------------------------------------------------------------------------------------------------------------------|\n| `signal` | A `[..., samples, ...]` `Tensor`. The rank and dimensions may be unknown. Rank must be at least 1. |\n| `frame_length` | The frame length in samples. An integer or scalar `Tensor`. |\n| `frame_step` | The frame hop size in samples. An integer or scalar `Tensor`. |\n| `pad_end` | Whether to pad the end of `signal` with `pad_value`. |\n| `pad_value` | An optional scalar `Tensor` to use where the input signal does not exist when `pad_end` is True. |\n| `axis` | A scalar integer `Tensor` indicating the axis to frame. Defaults to the last axis. Supports negative values for indexing from the end. |\n| `name` | An optional name for the operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` of frames with shape `[..., num_frames, frame_length, ...]`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------------------------------------------------|\n| `ValueError` | If `frame_length`, `frame_step`, `pad_value`, or `axis` are not scalar. |\n\n\u003cbr /\u003e"]]