1.利用声卡播放音频信号

function sine_gen
N = 1000; % 每一次产生的长度 
fs = 16e3; % 采样率16kHz 
fc = 400; % 正弦波频率 
dac = SoundCardDAC(fs); %构造dac对象 
onCleanup(@dac.delete); %注册善后函数 
while 1 
    t = (0:N-1)'/fs; % 时间t 
    y = sin(2*pi*fc*t); % 产生的信号 
    dac.tx(y); 
end
end
classdef SoundCardDAC < handle
    % SoundCardDAC 利用声卡将数字信号转为模拟波形
    %   利用obj = SoundCardDAC(fs)构造对象,其中fs为采样率,单位Hz
    %   类方法: (1) tx(signal) 将信号signal添加到声卡缓存并进行播放,该函数可产生连续的信号输出
    %           (2) tx_once(signal) 直接播放信号signal,该函数仅产生一段突发的信号
    %           (3) delete 释放占用的资源,可在主函数中利用onCleanup调用
    
    properties (SetAccess = private, GetAccess = private)
        m_ao = 0;
        m_fs = 44100;
        m_state = 0;    % 0: uninitialize, 1: ready, 2: runing
    end
    
    methods
        function obj = SoundCardDAC(fs) 
            obj.m_fs = fs;
            obj.m_ao = analogoutput('winsound');
            addchannel(obj.m_ao,1);
            set (obj.m_ao, 'SampleRate', obj.m_fs);
            obj.m_state = 1;
            fprintf(1,'Info: Create soundCardDAC object, fs=%d.\n', fs);
        end
        
        function tx(obj, signal)
            sample_th1 = obj.m_fs * 0.4;  % 保证声卡中至少有0.4s左右长度的数据
            sample_th2 = obj.m_fs * 0.6;  % 通常声卡缓冲区数据也不用太长
            
            signal = reshape(signal, length(signal), 1);    % 转为列向量
            
            if obj.m_state == 0  % not allowed
                error('Error: SoundCard does not be initialized.\n');
                return;
            end
            
            putdata(obj.m_ao, signal);
            sample_av = get(obj.m_ao, 'SamplesAvailable');

            if obj.m_state == 1 % ready
                if sample_av > sample_th1
                    obj.m_state = 2;
                    start(obj.m_ao);
                    fprintf(1,'Info: Starting DAC...\n');
                end
            elseif obj.m_state == 2 % running
                if sample_av < sample_th1   % 缓冲区数据量小于阈值,预示有可能程序实时性不够
                    fprintf(1,'Warning: Program efficiency may be too low.\n');
                elseif sample_av > sample_th2 % 数据太快,让程序停顿一会儿
                    time_pause = (sample_av-sample_th2)/obj.m_fs;
                    pause(time_pause);
                end
            else
                error('Error: Unknown error occured.\n');
            end
        end
        
        function tx_once(obj, signal)
            signal = reshape(signal, length(signal), 1);    % 转为列向量
           
            if obj.m_state == 0  % not allowed
                error('Error: SoundCard does not be initialized.\n');
            elseif obj.m_state == 1 % ready
                   putdata(obj.m_ao, signal);
                sample_av = get(obj.m_ao, 'SamplesAvailable');
                start(obj.m_ao);
                time_pause = sample_av/obj.m_fs + 0.2;
                pause(time_pause);
                stop(obj.m_ao);
            elseif obj.m_state == 2
                error('Error: Do not call tx() and tx_once() simultaneously.\n');
            else
                error('Error: Unknown error occured.\n');
            end
        end
                
        function delete(obj)
            if obj.m_ao ~= 0
                stop(obj.m_ao);
                delete(obj.m_ao);
                obj.m_ao = 0;
                obj.m_state = 0;
                fprintf(1,'Info: Destory soundCardDAC object.\n');
            end
        end 
    end
    
end

2.蒙特卡洛模拟求阴影面积

syms x0;
y0=int(sqrt(-x0^2+2*x0)*2,0,0.5)+int(sqrt(1-x0^2)*2,0.5,1);

tic;
N = 1e8; % 投掷次数 
s_squre = 1*sqrt(3); 
hit = 0; % 落在阴影面积内的概率 
for k = 1:N 
    x = rand() ; % x坐标均匀分布于[0,1] 
    y = (rand() *2 -1)*sqrt(3)/2; % y坐标均匀分布于[-sqrt(3)/2,sqrt(3)/2] 
    if x*x + y*y < 1 && (x-1)*(x-1) + y*y < 1
        hit = hit + 1; % 若(x,y)落在阴影面积内 
    end
end
prob = hit/N; % 求解落在阴影面积中的概率 
s_circle = s_squre* prob % 求阴影面积
toc;

 

posted on 2015-10-02 19:10  planet  阅读(360)  评论(0编辑  收藏  举报