Skip to content

Commit

Permalink
allow to use input envelope in noise sound
Browse files Browse the repository at this point in the history
  • Loading branch information
gisogrimm committed May 3, 2024
1 parent e99a0fb commit 06cd0e0
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions scripts/noisesound.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function noisesound( sInput, sOutput, dur )
function noisesound( sInput, sOutput, dur, tauenv )
% NOISESOUND - create a random-phase noise signal with input spectrum
%
% Usage:
% 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
Expand All @@ -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 )
Expand Down

0 comments on commit 06cd0e0

Please sign in to comment.