From 06cd0e0a4c4237cc0b03031232cf131ee9513105 Mon Sep 17 00:00:00 2001 From: Giso Grimm Date: Fri, 3 May 2024 16:02:42 +0200 Subject: [PATCH] allow to use input envelope in noise sound --- scripts/noisesound.m | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/scripts/noisesound.m b/scripts/noisesound.m index 2f7fc631..3aadb15d 100644 --- a/scripts/noisesound.m +++ b/scripts/noisesound.m @@ -1,4 +1,4 @@ -function noisesound( sInput, sOutput, dur ) +function noisesound( sInput, sOutput, dur, tauenv ) % NOISESOUND - create a random-phase noise signal with input spectrum % % Usage: @@ -6,7 +6,7 @@ function noisesound( sInput, sOutput, dur ) % % sInput : name of input sound file % sOutput: name of output sound file -% dur : duration (and fft length) of the output sound file +% dur : duration (and fft length) of the output sound file. If dur==0 then the original duration is used. % % Only the first channel of the input signal is used. The absolute % level of the output signal is not matched with the input @@ -16,16 +16,31 @@ function noisesound( sInput, sOutput, dur ) % of non-overlapping fragments of length duration is used. % % Author: Giso Grimm -% Date: 1/2014 +% Date: 1/2014, 2024 ; [x,fs] = audioread(sInput); - dur = round(dur*fs); + if dur > 0 + dur = round(dur*fs); + else + dur = size(x,1); + end + if nargin < 4 + tauenv = 0; + end x(:,2:end) = []; + rms_in = sqrt(mean(x.^2)); x = buffer(x,dur); + env = mean(abs(hilbert(x)),2); X = sqrt(mean(abs(realfft(x)).^2,2)); X = X .* exp(2*pi*i*rand(size(X))); x = realifft(X); - x = 0.9*x / max(abs(x(:))); + if tauenv > 0 + rms_out = sqrt(mean(x.^2)); + x = x .* (rms_in./rms_out); + x = x .* env; + else + x = 0.9*x / max(abs(x(:))); + end audiowrite(sOutput,x,fs); function y = realfft( x )