Communication principle Practice (i)--Audio signal processing

Source: Internet
Author: User

I. Discretization of the signal 1, sampling theorem:


– If the signal is limited and the sampling frequency FS exceeds twice times the maximum frequency of the signal, then the original continuous signal can be completely reconstructed from the sample sample.

Therefore, the sampling rate (FS) is a very important parameter in the simulation process. FS must meet twice times more than the maximum frequency of the signal.

E.g: produces a sine wave with a length of 1000 100Hz

; %= 8e3; %; %= (0: N-1) '/fs; %= sin (2*pi*fc*t); %ty plot xlabel (' time '); Ylabel (' amplitude ');

Second, the use of sound card to reproduce the signal 1, principle:

The sound card contains a DA converter that converts a digital signal to an analog signal

2, MATLAB provides the function

(1) First: Build a Soundcarddac object and specify the sample rate fs. Where the sampling rate is typically between 5000~96000hz, the typical value is 8kHz, 16kHz, 22.05kHz, 44.1kHz, 48kHz, 64kHz.

DAC = SOUNDCARDDAC (FS);

(2) and then register its aftercare method (need to call within the function, can not use the script)

Oncleanup (@dac.  Delete);
(3) using TX () or tx_once () for the digital-to-analog conversion, the signal amplitude should fall between [ -1,1] (function differences See m file description)
DAC.TX (signal);

Ps: Attach the object function code that the sound card encapsulates.

ClassDef Soundcarddac <Handle%SOUNDCARDDAC using a sound card to convert digital signals to analog waveforms% using obj=SOUNDCARDDAC (FS) constructs an object where FS is the sample rate, unit Hz% class Method: (1) TX (signal) Adds the signal signal to the sound card cache and plays it, which produces a continuous signal output%           (2) tx_once (signal) directly plays the signal signal, which produces only a burst of signal%           (3)Deletefrees up the resources used to invoke the properties in the main function using Oncleanup (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; %ensure that the sound card has at least 0.4s or so length of data sample_th2= Obj.m_fs *0.6; %usually the sound card buffer data is not too long signal= Reshape (signal, length (signal),1); %convert to column vectorifObj.m_state = =0%Not allowed error ('Error:soundcard does not being initialized.\n'); return;            End PutData (Obj.m_ao, signal); Sample_av=Get(Obj.m_ao,'samplesavailable'); ifObj.m_state = =1% ReadyifSample_av >sample_th1 obj.m_state=2;                    Start (Obj.m_ao); fprintf (1,'info:starting dac...\n'); End ElseIf Obj.m_state==2%RunningifSample_av < sample_th1%Buffer data volume is less than the threshold value, indicating that the program may not be real-time fprintf (1,'warning:program efficiency may too low.\n'); ElseIf Sample_av> Sample_th2%The data is too fast for the program to pause for a while time_pause= (sample_av-sample_th2)/Obj.m_fs;                Pause (Time_pause); EndElseError ('Error:unknown Error occured.\n'); End End Function tx_once (obj, signal) signal= Reshape (signal, length (signal),1); %convert to column vectorifObj.m_state = =0%Not allowed error ('Error:soundcard does not being 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==2Error ('Error:do Not Call TX () and Tx_once () simultaneously.\n'); ElseError ('Error:unknown Error occured.\n'); End End FunctionDelete(obj)ifObj.m_ao ~=0Stop (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

Use this object-oriented demo: (Note to ensure phase continuity)

function Sine_gen    ; % each time the   resulting length = 16e3;% sample rate 16kHz   ;% sine wave frequency    = SOUNDCARDDAC (fs);%  Constructs a DAC object   oncleanup (@dac. delete); % Registration Aftercare function    while 1     = (0: N)'     y = sin (2*pi*fc*t);% generated signal     dac.tx (y);   EndEnd

Communication principle Practice (i)--Audio signal processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.