Matlab计算采样频率以从纯正弦波产生特定声音
Matlab computing the sampling frequency to produce a specific sound from a pure sine wave
我有一个很奇怪但我觉得很有趣的问题。这个想法是为了在播放音频时更好地理解采样率与频移。这个想法是一个小实验:
clc;全部清除;
%synthetic example
%in practice it seems that a period T = 2*maxFreq is not enough -> i choose
%10
T=1/(10*10^6);%period should be at least 1/(2*10^6Hz) => Nyquist freq if we want to be able to reproduce 10^5Hz max freq
x=0:T:1-T;
f=10^6;%frequency
y=sin(2*pi*f*x);
%i see visually that there is 11 samples constituting 1 period
%plot(y(1:11))
%plot(y(100:111))
%etc
nbPeriods=length(y)/11;%nbtotalsamples/nbsamplesOf1Period
%y contains 10^6 oscillations each of 11 samples
%therefore if i want to reproduce a 1Khz sound, I compute my sampling frequency :
% Fs = nbPeriods/10^3
Fs=909.09;
a=audioplayer(y,Fs)
tic;
play(a)
toc;
objective 是以正确的采样频率 Fs 播放此正弦波 y,以获得 1KHz 的感知(音频从扬声器输出)。
我的想法是生成一个非常高频的正弦波,这里是 10^6 Hz,然后以采样频率 Fs 播放它,这样我们就可以获得 1KHz。我计算出我需要 Fs = 909.09,但是 Matlab 拒绝了,我在控制台中收到此错误消息:
a =
audioplayer with properties:
SampleRate: 909.0900
BitsPerSample: 16
NumberOfChannels: 1
DeviceID: -1
CurrentSample: 1
TotalSamples: 10000000
Running: 'off'
StartFcn: []
StopFcn: []
TimerFcn: []
TimerPeriod: 0.0500
Tag: ''
UserData: []
Type: 'audioplayer'
Error using audioplayer/resume (line 766)
Device Error: Invalid sample rate
Error in audioplayer/play (line 125)
obj.resume();
Error in sineExample (line 25)
play(a)
也许我的推理是错误的。有人可以帮我想想 this/clarify/correct 我的(潜在)错误吗?
令人困惑的手册状态:
Fs: Sampling rate in Hz. Valid values depend on both the sample rates
permitted by MATLAB® and the specific audio hardware on your system.
MATLAB has a hard restriction of 1000 Hz <= Fs <= 384000 Hz, although
further hardware-dependent restrictions apply. Typical values
supported by most sound cards are 8000, 11025, 22050, 44100, 48000,
and 96000 Hz.
您输入的采样率低于 1000Hz。
但是正如您在评论中提到的,它似乎接受低至 80。这似乎是真的。
然而,错误在play
。您只能播放声卡接受的频率。这就是为什么文档的最后一部分指出有更多限制的原因,因为您的声卡仅针对一组非常特定的采样率而设计,不能以任意频率播放。
我有一个很奇怪但我觉得很有趣的问题。这个想法是为了在播放音频时更好地理解采样率与频移。这个想法是一个小实验: clc;全部清除;
%synthetic example
%in practice it seems that a period T = 2*maxFreq is not enough -> i choose
%10
T=1/(10*10^6);%period should be at least 1/(2*10^6Hz) => Nyquist freq if we want to be able to reproduce 10^5Hz max freq
x=0:T:1-T;
f=10^6;%frequency
y=sin(2*pi*f*x);
%i see visually that there is 11 samples constituting 1 period
%plot(y(1:11))
%plot(y(100:111))
%etc
nbPeriods=length(y)/11;%nbtotalsamples/nbsamplesOf1Period
%y contains 10^6 oscillations each of 11 samples
%therefore if i want to reproduce a 1Khz sound, I compute my sampling frequency :
% Fs = nbPeriods/10^3
Fs=909.09;
a=audioplayer(y,Fs)
tic;
play(a)
toc;
objective 是以正确的采样频率 Fs 播放此正弦波 y,以获得 1KHz 的感知(音频从扬声器输出)。
我的想法是生成一个非常高频的正弦波,这里是 10^6 Hz,然后以采样频率 Fs 播放它,这样我们就可以获得 1KHz。我计算出我需要 Fs = 909.09,但是 Matlab 拒绝了,我在控制台中收到此错误消息:
a =
audioplayer with properties:
SampleRate: 909.0900
BitsPerSample: 16
NumberOfChannels: 1
DeviceID: -1
CurrentSample: 1
TotalSamples: 10000000
Running: 'off'
StartFcn: []
StopFcn: []
TimerFcn: []
TimerPeriod: 0.0500
Tag: ''
UserData: []
Type: 'audioplayer'
Error using audioplayer/resume (line 766)
Device Error: Invalid sample rate
Error in audioplayer/play (line 125)
obj.resume();
Error in sineExample (line 25)
play(a)
也许我的推理是错误的。有人可以帮我想想 this/clarify/correct 我的(潜在)错误吗?
令人困惑的手册状态:
Fs: Sampling rate in Hz. Valid values depend on both the sample rates permitted by MATLAB® and the specific audio hardware on your system. MATLAB has a hard restriction of 1000 Hz <= Fs <= 384000 Hz, although further hardware-dependent restrictions apply. Typical values supported by most sound cards are 8000, 11025, 22050, 44100, 48000, and 96000 Hz.
您输入的采样率低于 1000Hz。
但是正如您在评论中提到的,它似乎接受低至 80。这似乎是真的。
然而,错误在play
。您只能播放声卡接受的频率。这就是为什么文档的最后一部分指出有更多限制的原因,因为您的声卡仅针对一组非常特定的采样率而设计,不能以任意频率播放。