pwmsound.m

来自「脉冲宽度调制(PWM)是英文“Pulse Width Modulation”的缩」· M 代码 · 共 49 行

M
49
字号
function PWMSound(Tp,Tcycle)
%
% Tp:       Pulse Width [sec]   typ.: 0.6-2.8e-3
% Tcycle:   Cycle Time [sec]    typ.: 10e-3
%
% create a pwm like output from the soundcard
% make sure volume control is set to maximum
% 
% stop:     Tp<0, or Tcycle<0
%
% mwunder@mathworks.de, December 2005
%
persistent ao Fs

%% reset
if Tcycle*Tp<0
    stop(ao);
    delete ao
    clear ao Fs
    return
end

%% create new ao object
if length(ao)>0
    stop(ao);
else
    ao=analogoutput('winsound');
    addchannel(ao,1:2);
    % Sample Rate for SoundCard
    Fs=22050;

    set(ao,...
        'SampleRate',Fs,...
        'RepeatOutput',inf);
end

% cycle time typically: 10ms
ncycle=Tcycle*Fs;

len=ceil(Fs*Tp);
dat=[ones(1,len) -ones(1,len) zeros(1,ceil(ncycle-2*len))];

% same for both channels 
% two channels planned for future
putdata(ao,[dat' dat'])
start(ao)

return

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?