audiotrim

by Tim C. Lueth, SG-Lib Toolbox: SolidGeometry 5.6 - Video/Audio/PDF
Introduced first in SolidGeometry 4.9, Creation date: 2020-03-26, Last change: 2025-09-14

trims an audio signal and adds a short break



See Also: soundTL , audiorecorderTL , audiovocoder

Example Illustration

 missing image of audiotrim(y,FS,n,thr)

Syntax

y=audiotrim(y,FS,[n,thr])

Input Parameter

y: samples
FS: Sample rate
n: seconds to add silence; default is 0.2
thr: threshold for silence; default is 0.01

Output Parameter

y: sample signal

Examples


y=audiorecorderTL('PRO X',16000,5); audiotrim(y,16000); y=ans; soundTL(y,16000,'pro x');




Copyright 2020-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth

Algorithm (Workflow)

This algorithm is designed to trim an audio signal and add a short break of silence at the end. It is part of the SolidGeometry library and was introduced in version 4.9.

Input Parameters

Output

Algorithm Steps

  1. Calculate the original length of the audio signal in seconds using ol = size(y,1)/FS.
  2. Retrieve the parameter n using getfuncparams, defaulting to 0.2 seconds, and convert it to samples by multiplying with FS.
  3. Retrieve the parameter thr using getfuncparams, defaulting to 0.01.
  4. Find indices xi where the absolute value of the audio signal is less than the threshold thr.
  5. Calculate ai as the difference between consecutive indices in xi using circshift.
  6. Identify the last index bi where the difference is greater than zero, indicating the end of a silent segment.
  7. If bi is not empty, trim the audio signal at the last silent index lst and append n samples of silence.
  8. If bi is empty, set the output y to an empty array.
  9. Calculate the new length of the audio signal in seconds using nl = size(y,1)/FS.
  10. If no output is requested, print the reduction in audio length and play the sound using sound(y,FS).
Algorithm explaination created using ChatGPT on 2025-08-19 01:01. (Please note: No guarantee for the correctness of this explanation)

Last html export of this page out of FM database by TL: 2025-09-21