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

时间:2016-09-22 16:16:53

标签: matlab audio click phase

我想通过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);

我很感激解决这些问题的任何帮助。

2 个答案:

答案 0 :(得分:1)

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

  1. 我们创建一个矢量,其元素与最终信号一样多。向量的每个元素都保持最终信号在该特定样本中应具有的频率。
  2. 我们使用fmmod函数创建1Hz信号,该信号由我们在1中创建的矢量调制。
  3. 我尝试调整您的代码并尽可能保持相似。但是,所有持续时间现在都是几秒钟。

    请注意,我假设各个段之间的边缘都可以。所以,如果你有一个段结束,例如,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);
    

答案 1 :(得分:0)

我从Dik J. Hermes那里得到了另一个解决这个问题的方法,我也将分享。这里的优点是,人们不依赖于fmmod功能(上述解决方案所需),只能在商业工具箱中使用。

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