Report: Data acquisition and Signal Conditioning

advertisement
Report: Data acquisition
and Signal Conditioning
Raniya Parappil z3331030 , Zain Shah z3258101
[Pick the date]
[Type the abstract of the document here. The abstract is typically a short summary of the contents
of the document. Type the abstract of the document here. The abstract is typically a short summary
of the contents of the document.]
Introduction:
A system takes an input (sound signal) and transforms it into an output quantity (waveform or
spectrum) that can be observed or recorded. The waveform contains information about the
magnitude, which indicates the size of the input and the frequency. Digital signals are useful
when data acquisition and processing are performed by using a digital computer. Signals may
be characterized as either static or dynamic. A dynamic signal is defined as a time-dependent
signal.
it is possible to separate a complex signal into a number of sine and cosine functions This
representation of a signal as a series of sines and cosines is called a Fourier series. It allows
essentially all mathematical functions to be represented by an infinite series of sines and
cosines.
The
Fourier
transform
has
a
magnitude
and
a
phase,
phase:
Filter
A filter is used to remove undesirable frequency information from a dynamic signal. Filters
can be classified as low pass, high pass, bandpass, and notch. A low-pass filter allows
frequencies below the required cut-off frequency to pass and block signals above the cut off.
A high-pass filter allows only frequencies above the cutoff frequency to pass. A band pass
filter is described by a low cutoff frequency, fc1, and a high cutoff frequency, fc2 to define a
band of frequencies that are allowed to pass. A notch filter permits the passage of all
frequencies except those that have a narrow frequency. Filters perform a mathematical
operation on the input signal as commanded by the transfer function. Passive analog filter
circuits consist of resistors, capacitors, and inductors.
In this exercise, the “ring tone” of a mobile phone was captured and the analysed. A
waveform plot in the time domain and a spectral plot in the frequency domain are plotted. A
“band-pass filter” was used to remove the noise while passing only the wanted frequency of
the ring tone.
In order to capture the ringtone, a system design similar to the one shown below was used.
The mobile phone generates a ring tone and the sound signal is then captured by a
microphone. The signal is further passed to a sound card and Matlab contains a “data
acquisition toolbox” for the capture of the sound signal into the workspace.
Using the GUI, the ring tone peak amplitude and the frequency are shown. Since disturbances
caused by the noises in the environment cannot be avoided therefore, band-passing filtering
of the captured signal is required.
The GUI includes the following functions:
1. Graph plotting area
2. Selection between: waveform and spectrum
3. Choice of acquisition duration
4. Start of data acquisition
5. Application of filtering
Mathlab code used:
function varargout = GUI(varargin)
% GUI M-file for GUI.fig
%
GUI, by itself, creates a new GUI or raises the existing
%
singleton*.
%
%
H = GUI returns the handle to a new GUI or the handle to
%
the existing singleton*.
%
%
GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%
function named CALLBACK in GUI.M with the given input arguments.
%
%
GUI('Property','Value',...) creates a new GUI or raises the
%
existing singleton*. Starting from the left, property value pairs
are
%
applied to the GUI before GUI_OpeningFcn gets called. An
%
unrecognized property name or invalid value makes property
application
%
stop. All inputs are passed to GUI_OpeningFcn via varargin.
%
%
*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%
instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI
% Last Modified by GUIDE v2.5 28-Aug-2011 16:03:08
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_OpeningFcn, ...
'gui_OutputFcn', @GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback',
[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUI is made visible.
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject
handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% varargin
command line arguments to GUI (see VARARGIN)
clc;%clear command window
% Choose default command line output for GUI
handles.output = hObject;
handles.S = 0;
handles.W = 1;
handles.time = 1;
set(handles.Waveform, 'Value',handles.W);
set(handles.Start,'enable','off');%disable Start until end of acquistion
dur.sec=2;%default acquisition duration, dur is a structure
snd.rate=8000;%sound card sampling rate, snd is a structure
snd.AI = analoginput('winsound');%create sound object
addchannel(snd.AI,1);%1 sound channel
set(snd.AI,'SampleRate',snd.rate);%setup sound card
set(snd.AI,'SamplesPerTrigger',dur.sec*snd.rate);%setup
start(snd.AI); wait(snd.AI,dur.sec+0.5);%start acquisition
snd.data=getdata(snd.AI);%get data from sound card
snd.data=OffsetData(snd.data);%offset data to its mean value
delete(snd.AI); clear snd.AI;%finished acquistion
PlotWaveform(dur,snd);%plot the default waveform
set(handles.Start,'enable','on');%enable Start pushbutton
handles.snd=snd;
handles.dur=dur;%store data
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
function [data]=OffsetData(data)
data=data-mean(data);
function []=PlotWaveform(dur,snd)
cla;
axis auto
dur.time=1/snd.rate:1/snd.rate:dur.sec;
plot(dur.time,snd.data,'b'); grid on; hold on;
title('Original Signal');
xlabel('Time(sec)');
ylabel('Amplitude (volt)');
function []=PlotSpectrum(snd)
cla;
periodogram(snd.data,[],2048,snd.rate);
[spe.Pxx,spe.F]=periodogram(snd.data,[],2048,snd.rate);
[mx,ix]=max(spe.Pxx); logmx=10*log10(mx);
title(sprintf('Max power density=%6.3f at
%04.0fHz',logmx,spe.F(ix)),'color','r');
mlogmx=mean(10*log10(spe.Pxx)); dmx=abs(mlogmx)/2;
ax=axis; axis([ax(1:2) mlogmx-dmx mlogmx+dmx]);
dmx
function []= PlotWaveformFiltered(dur,snd)
cla;
axis auto
dur.time=1/snd.rate:1/snd.rate:dur.sec;
plot(dur.time,snd.data,'b'); grid on; hold on;
plot(dur.time,snd.filter,'r'); hold off;
title('Filtered Waveform Signal');
xlabel('Time(sec)');
ylabel('Amplitude (volt)');
function []=PlotSpectrumFiltered(snd)
cla;
[spe.Pxx,spe.F]=periodogram(snd.data,[],2048,snd.rate);
[mx,ix]=max(spe.Pxx); %logmx=10*log10(mx);
mlogmx=mean(10*log10(spe.Pxx)); dmx=abs(mlogmx)/2;
spe.Pxx=10*log10(spe.Pxx);
plot(spe.F,spe.Pxx,'b');hold on;grid on;
axis auto;
[spe.Pxx2,spe.F2]=periodogram(snd.filter,[],2048,snd.rate);
spe.Pxx2=10*log10(spe.Pxx2);
plot(spe.F2,spe.Pxx2,'r')
title(sprintf('Filtered Spectrum Signal','color','r'));
ax=axis; axis([ax(1:2) mlogmx-dmx mlogmx+dmx]);
hold off;
% --- Outputs from this function are returned to the command line.
function varargout = GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject
handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in Time.
function Time_Callback(hObject, eventdata, handles)
% hObject
handle to Time (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
handles.time = get(handles.Time,'Value');
guidata(hObject, handles);
% Hints: contents = cellstr(get(hObject,'String')) returns Time contents as
cell array
%
contents{get(hObject,'Value')} returns selected item from Time
% --- Executes during object creation, after setting all properties.
function Time_CreateFcn(hObject, ~, handles)
% hObject
handle to Time (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
%
See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject
handle to Start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
if handles.time==1
dur.sec=2;%default acquisition duration, dur is a structure
end
if handles.time ==2
dur.sec=5;
end
if handles.time==3
dur.sec=10;
end
snd.rate=8000;%sound card sampling rate, snd is a structure
snd.AI = analoginput('winsound');%create sound object
addchannel(snd.AI,1);%1 sound channel
set(snd.AI,'SampleRate',snd.rate);%setup sound card
set(snd.AI,'SamplesPerTrigger',dur.sec*snd.rate);%setup
start(snd.AI); wait(snd.AI,dur.sec+0.5);%start acquisition
snd.data=getdata(snd.AI);%get data from sound card
snd.data=OffsetData(snd.data);%offset data to its mean value
delete(snd.AI); clear snd.AI;%finished acquistion
handles.snd=snd;
handles.dur=dur;%store data
%Plot depending on selected toggle
if handles.W == 1
PlotWaveform(dur,snd);%plot the default waveform
end
if handles.S == 1
PlotSpectrum(snd);%plot the default spectrum
end
guidata(hObject, handles);
% --- Executes on button press in Filter ~
function Filter_Callback(hObject, eventdata, handles)
% hObject
handle to Filter (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
if length(handles.snd.data)>0,
dur=handles.dur; snd=handles.snd; Fs=snd.rate;
A_stop1 = 30; % Attenuation in the first stopband = 30 dB
F_stop1 = 800; % Edge of the stopband = 800 Hz
F_pass1 = 1000; % Edge of the passband = 1000 Hz
F_pass2 = 2000; % Closing edge of the passband = 2000 Hz
F_stop2 = 2200; % Edge of the second stopband = 2200 Hz
A_stop2 = 30; % Attenuation in the second stopband = 30 dB
A_pass = 1; % Allowed ripple in the passband = 1 dB
BandPassSpecObj =
fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',F_stop1,F_pass1,F_pass2,F
_stop2,A_stop1,A_pass,A_stop2,Fs);
BandPassFilt = design(BandPassSpecObj, 'butter');
dur.time=1/snd.rate:1/snd.rate:dur.sec;
z=find(isnan(snd.data)); snd.data(z)=[]; dur.time(z)=[];
snd.filter=filter (BandPassFilt,snd.data);
if get(handles.Waveform,'value')==1,
PlotWaveformFiltered(dur,snd);
end;
if get(handles.Spectrum,'value')==1,
PlotSpectrumFiltered(snd);
end;
end;
guidata(hObject, handles);
% --- Executes on button press in Waveform.
function Waveform_Callback(hObject, eventdata, handles)
% hObject
handle to Waveform (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
snd=handles.snd;
dur=handles.dur;
cla;
handles.W = get(hObject,'Value');
S = get(handles.Spectrum,'Value');
if handles.S ==1 || S==1
set(handles.Spectrum,'Value',0);
handles.S=0;
cla;
end
if handles.W ==1
handles.axes1;
PlotWaveform(dur,snd);
end
guidata(hObject, handles);
% Hint: get(hObject,'Value') returns toggle state of Waveform
% --- Executes on button press in Spectrum.
function Spectrum_Callback(hObject, eventdata, handles)
% hObject
handle to Spectrum (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
snd=handles.snd;
cla;
handles.S = get(hObject,'Value');
W = get(handles.Waveform,'Value');
if handles.W ==1 || W ==1
handles.W =0;
set(handles.Waveform,'Value',0);
end
if handles.S ==1
PlotSpectrum(snd);
end
guidata(hObject, handles);
% Hint: get(hObject,'Value') returns toggle state of Spectrum
Discussion:
When the GUI starts the “waveform” and “spectrum” is shown. The start button is pressed
and the ringtone frequency is with the chosen time frame (2, 5 or 10 seconds) and a wave is
plotted. The GUI requires user interaction to determine its course of action. Both Spectrum
and waveform are both “unchecked” at start. The duration the sound is recorded from the
microphone is chosen form the drop down menu. The function reads the selected value using
the ‘get’ function, and then sets the scalar value ‘handles.dur’.
The start function reads in the selected data collected duration (handles.dur), and then collects
data from the sound channel. Once the required data is collected in the selected time frame, it
then checks the value of handles.waveform and handles.spectrum, and then passes the
collected data to either the plotwaveform function or the plotspectrum function, by ‘if’ comm
ands.
The Matlab code disables the start button until GUI obtains sound data for the ticked time
frame in the popup box. Then the program plots the “waveform” unfiltered spectrum and
saves the information inside handles, since handles could be transferred between functions of
the same program without loss of data. Two handles are created, one for waveform and the
other for spectrum. Initially these handles are assigned values such that waveform box is
ticked whereas the spectrum handles is off.
The waveform calls the function assigning the handles.waveform a value of 0 if it is
unchecked by user This function starts off with clearing the plot, check whether handles.W is
1 or 0 and plots a waveform. By using “if” command; if the waveform checkbox value is 1
then handles.waveform=1 and handles.spectrum=0. The program also sets the value of the
unchecked checkbox to 0 when the one checkbox is ticked.
The waveform function (using the plot function to draw the graph) setups up a vector of
values, from 0 to the end value then plotting the vector against collected amplitudes of the
sounds. The maximum value is also found and is shown on the top of the graph. The
waveform plot filtered function is used when the unfiltered waveform plot function, graphs
the unfiltered data using “plot”. The unfiltered data is plotted first followed by the filtered
data being plotted once the filter button is pressed. [See figure below showing a signal
plotted using the waveform plot function]
The filter function analyses the sound data, by checking the length of the snd.data vector. If
there is data, the function then filters the data. After filtering the data is saved in
handles.snd.filter. This saved data is passed to waveform plot function or the filtered
spectrum function. The GUI programming is getting a filtered signal from the original signal.
The figure below is the model (taken from lecture notes week 4) used to create a filter
function for the GUI. In the matlab pass band are 1000 Hz and 2000 Hz and the edges of
stopband are 800 Hz and 2200 Hz. The attenuation is 30 dB in both while a ripple of 1 dB is
allowed in GUI programming. These could be changed to form different filters by varying the
F_pass.
The figure below shows a waveform plot filtered (the graph red) using the filter command.
The spectrum plot function in Matlab is a periodogram function in the Matlab program. The
handles.snd structure is passed into the spectrum plot and , and snd.data and snd.rate are
passed into the periodogram function to create the spectrum graph. This determines the
maximum power density for the data.
There is a difference between the filtered and the unfiltered spectrum plot. The resultant
vectors are passed to spe.Pxx and spe.F and then the plot function is used to plot these values
so that the periograms result is mirrored. These desired results are obtained by using the
snd.data vector in blue followed by the snd.filter which is overlaid in red. The GUI operation
comes to an end here even though it still is able to indefinitely collect and display
information. [See figure below showing a signal plotted using the spectrum plot function]
Conclusion:
This experiment was conducted to calculate the peak amplitude, corresponding density and
the maximum power density of the phones ring tone. Ringtone of mobile phone is classified
as dynamic signal. This signal had a non-deterministic dynamic character. Band type filters
were used to remove the noise from the output signal (ringtone|). In spectrum plot it can be
noticed that the signal magnitude at high frequency decreases which occurs due to the fact
that microphone is insensitive to high frequencies.
Download