⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwmsound.m

📁 脉冲宽度调制(PWM)是英文“Pulse Width Modulation”的缩写
💻 M
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -