John Doe
John Doe

Reputation: 83

Matlab computing the sampling frequency to produce a specific sound from a pure sine wave

I have a rather strange but i thin interesting question. The idea is to gain a better understanding of sampling rate vs frequency shifting when playing audio. The idea is a little experiment: clc;clear all;

%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;

the objective is to play this sine wave y at the correct sampling frequency Fs in such as way as to obtain a perceptually (audio going out from the speakers) of 1KHz.

My idea is to generate a very high frequency sine, here 10^6 Hz, and then play it at a sampling freq Fs such that we obtain 1KHz. I computed that i need Fs = 909.09, however Matlab refuses that and i get this error message in console:

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)

Maybe my reasoning is wrong. Can someone help me to think about this/clarify/correct my (potential) mistakes?

Upvotes: 0

Views: 252

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

Confusingly the manual states:

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.

Yet you are inputting a sample rate lower than 1000Hz.

But as you mention in your comment that it seem to accept as low as 80. This seems to be true.

However, the error is in play. You can only play the frequencies your audio card accepts. This is the reason why that last part of the documentation states that there are more restrictions, as your audio card is only designed for a set of very specific sample rates and can not play at an arbitrary frequency.

Upvotes: 1

Related Questions