MATLAB Answers

# How to add the level to a sine tone?

5 views (last 30 days)
Paul Hinze on 18 Apr 2021
Commented: Paul Hinze on 18 Apr 2021
Hi,
I already built the noise and the sine tone, but how can i add the level (in dB SPL) to the noise and sine tone?
% Set-up parameters
fs = 22050; % This is the sampling frequency of the Titan
stim_dur = 1.; % duration of puretone in seconds
noise_dur = 1.; % duration of noise in seconds
ISI_dur = .020; % ISI duration in seconds
ramp_dur = .010; % ramp duration in seconds
t = 0:1/fs:stim_dur-1/fs; % Time vector
f = 10e2; % Frequency
levels = 60:5:100; % Levels dB SPL
% Create noise
nsamples = floor(noise_dur*fs);
noise_all_Hz = randn(nsamples,1);
noise = bandpass(noise_all_Hz,[950 1050],fs);
noise = noise';
% Create 1000 Hz tone
tone = sin(2*pi*f*t);
% Setup ramp
rampSamps = floor(fs*ramp_dur); % Round toward minimum of sampling frequency and duration of ramp
window = hanning(2*rampSamps)'; % hanning window is cosine^2 this will change depending on the kind of ramp you want
w1 = window(1:ceil((length(window))/2)); % use the first half of hanning function for onramp
w2 = window(ceil((length(window))/2)+1:end); % use second half of hanning function of off ramp
w_on_tone = [w1 ones(1,length(tone)-length(w1))]; % Create rising/starting ramp for tone
w_off_tone = [ones(1,length(tone)-length(w2)) w2]; % Create falling/ending ramp for tone
w_on_noise = [w1 ones(1,length(noise)-length(w1))]; % Create rising/starting ramp for noise
w_off_noise = [ones(1,length(noise)-length(w2)) w2]; % Create falling/ending ramp for noise
% Ramp stimuli
noise_ramped = noise.*w_on_noise.*w_off_noise; % Add both ramps to the noise
tone_ramped = tone.*w_on_tone.*w_off_tone; % Add both ramps to the tone
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Jonas on 18 Apr 2021
the level of a sound is usually the root mean square of the amplitude values. going on from there db SPL is calculated as 20*log10(rmsValue/(20*10^-6)). if you reverse my description you get the factor with which you have to multiply your sound.
##### 2 CommentsShowHide 1 older comment
Paul Hinze on 18 Apr 2021
Thank you! I will try that

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!