如何通过 MATLAB 生成由不同频率滑音段组成的声音?

How to generate a sound that consists of different frequency-glide-segments via MATLAB?

我想通过 MATLAB 生成一种声音,它包含预定义数量的不同频率滑移(扫描)。为此我写了一段MATLAB代码。但是,我遇到了两个问题:

1) 当我播放声音时,声音在整个持续时间内都是咔嗒声。

-> 这可能与前后扫描段结束时的相位角不同有关。我试图解决这个问题(见下面我的代码) - 到目前为止没有成功。您将在这里看到这种声音的频谱图:

spectrogram of concatenated sound which is perceived with several 'soft' clicks

2) 当我发出声音时,除了这些更柔和的声音之外,有时还会有更明显的咔哒声。这在频谱图中清晰可见。 --> 在这里我不确定问题可能是什么以及如何避免它。

spectrogram of concatenated sound with additional distinct click

我生成声音的代码如下:

clear all;
close all;

%% define stimulus parameters
soundDuration = 1200; % duration of sound
sf = 44100; % sampling rate
ampl = 0.05; % 0.05; % ampl
segmentDuration = 25; % duration of one standard segment in ms
nSegments = soundDuration/segmentDuration; % number of segments of which the sound should consist of
t = 0:1/sf:(0.025-1/sf); % time vector for segment

%% generate sound consisting of n sweep-segments
complexSound = [];
for iSeg = 1:nSegments

    f1 = 1000:10:3000; 
    f1 = randsample(f1,1); % start freq in Hz for current sweep segment
    f2 = 1500:10:4500;
    f2 = randsample(f2,1); % end freq in Hz  for current sweep segment

    if iSeg == 1
        sweep = ampl * chirp(t,f1,segmentDuration/1000,f2,'logarithmic'); % generate sweep-segment withou considering the phase

    else
        sweep = ampl * chirp(t+1/sf,f1,segmentDuration/1000,f2,'logarithmic',ph); % the current sweep starts with a t+1/sf later and with the phase angle with which the previous sweep ended

    end
    ph = -90+360*(f2*t(end)+1/sf); % calculate the phase at the time point at which the current sweep ends and from that calculate the starting phase for the next sweep

    sweep = sweep';
    complexSound = [complexSound; sweep]; % concatenate sweep segments to form the complex sound

end
stim = complexSound; 
sound(stim,sf);

感谢任何帮助解决这些问题的帮助。

所以,基本上你想要的是调频。嗯,不是真的,但我们基本上可以滥用 matlab 为它提供的功能。诀窍是这样的:

  1. 我们创建一个包含与最终信号一样多的元素的向量。向量的每个元素都包含最终信号在该特定样本处应具有的频率。
  2. 我们使用 fmmod 函数创建一个 1Hz 信号,该信号由我们在 1 中创建的矢量调制。

我尝试调整您的代码并使其尽可能相似。但是,现在所有持续时间都以秒为单位。

请注意,我假设各个段之间的边缘没问题。因此,如果您有一段以 1500Hz 结束,而下一段以 2000Hz 开始,则这两者之间没有平滑的进展。也许 fmmod 函数可以稍微平滑一点,但我没有检查。

然而,这里是代码:

%% clear
clear all;
close all;

%% define stimulus parameters
soundDuration = 1.2; % duration of sound in seconds
sf = 44100; % sampling rate
ampl = 0.05; % 0.05; % ampl
segmentDuration = 0.025; % duration of one standard segment in s
nSegments = round(soundDuration/segmentDuration); % number of segments of which the sound should consist of
samples_per_segment = floor(sf * segmentDuration);

%% generate sound consisting of n sweep-segments
modulator = zeros(1, nSegments * samples_per_segment);
first_idx = 1;
for iSeg = 1:nSegments

    f1 = 1000:10:3000; 
    f1 = randsample(f1,1); % start freq in Hz for current sweep segment
    f2 = 1500:10:4500;
    f2 = randsample(f2,1); % end freq in Hz  for current sweep segment

    modulator(first_idx:first_idx + samples_per_segment-1) = logspace(log10(f1), log10(f2), samples_per_segment); % we add the logarithmic progression from f1 to f2 here
    first_idx = first_idx + samples_per_segment;

end
%% create final sound
stim = fmmod(modulator, 1, sf, 1); % here we abuse the frequency modulation function of matlab.
                                   % we basically tell it to create a 1Hz
                                   % sinewave and modulate it by the vector
                                   % we created in the previous step. the
                                   % last argument tells it basically that
                                   % if i give it a 1, it should increase
                                   % the frequency by 1Hz, if i give it a
                                   % 100, it should increase the freq by
                                   % 100Hz and so on...
stim = ampl .* stim; % multiply by the amplitude

%% plot...
spectrogram(stim,1000,[],[],sf,'yaxis'); % this provides a nice tf plot....

%% play
sound(stim,sf);

我从 Dik J. Hermes 那里收到了这个问题的另一个解决方案,我也将分享它。这里的优点是不依赖于仅在商业工具箱中可用的 fmmod 函数(上述解决方案需要)。

原始代码(位于本页顶部)中的问题是阶段管理不正确。下面的代码首先计算瞬时频率 insFreq(t)。如果 stim(t) = a*sin(phi(t)),则 instFreq = phi'(t)/2*pi,或 phi(t) = 2*pi*integral(instFreq)。 instFreq(t) 在 for 循环中计算。在 for 循环之后,通过对 instFreq 积分并乘以 2*pi 来计算 phi(t)。通过这种方式,人们会注意信号刺激本身是连续的并且不包含相位跳跃。由于瞬时频率的突然变化,stim 的时间导数在从一段到另一段的过渡处是不连续的。这将导致一些小的基本点击,这是无法避免的(至少不是这里使用的参数(f1,f2,段持续时间)。

clear all;
close all;

% define stimulus parameters
soundDuration = 1.2; % duration of sound
sf = 44100; % sampling rate
dt = 1/sf;
ampl = 0.05; % 0.05; % ampl
segmentDuration = 0.025; % duration of one segment in s
nSegments = round(soundDuration/segmentDuration); % number of segments of which the sound should consist of
tSegment = 0:dt:(segmentDuration-dt); % time vector for segment

% generate sound consisting of n sweep-segments
complexSound = [];
logInstFreq = [];
startFreqArray = 1000:10:3000;
endFreqArray = 1500:10:4500;
p = tSegment/tSegment(end);
for iSeg = 1:nSegments
    f1 = randsample(startFreqArray,1); % start freq in Hz for current sweep segment
    f2 = randsample(endFreqArray,1); % end freq in Hz  for current sweep segment
    logf1 = log10(f1);
    logf2 = log10(f2);
    logInstFreq = [logInstFreq (logf2-logf1)*p+logf1]; 
end
instFreq = 10.^logInstFreq;
phi = 2*pi*cumsum(instFreq)*dt;
stim = ampl*sin(phi); 
t = 0:dt:(length(instFreq)-1)*dt;
plot(t, instFreq)
sound(stim,sf);
audiowrite('sound.wav', stim, sf);