Sunteți pe pagina 1din 23

Contents

1. Program a matlab function that generates sinusoidal signal. Using the function programmed generate a signal consisting of sum of n sinusoids. 3. Try different visualizations 4. Estimate PDF of the signals (hist) 5. Some extra exercises 6. Add to sinusoidal signal white noise generated by randn. Visualize the result with different signal to noise ratios S/N = [100 10 1 0.1] 7. Create simple digital filter using Signal Processing Toolbox 8. Use filtering to get better signal to noise ratio 9. Do 7. and 8. using MATLAB Signal Procesing Toolbox GUI sptool.
%Exercise on singlans and filters for course MIT-1210 %Script by Matti Jukola (SH101) %2009.10.13

1. Program a matlab function that generates sinusoidal signal.


%Uncomment following sectiond and paste it to sinisig.m (edit sinisig) % function x = sinsig(t, f, a, p) % %Input: t (vector) contains the sampling time instants % % f (scalar) frequency of the sinusoid % % a (scalar) amplitude of the sinusoid % % p (scalar) phase of the sinusoid % % % %Output: x (vector) output values of the sinusoid signal % %Name of author % %Date % x = a.*sin(2.*pi.*t.*f+p); %Q: 0; t = f = a = p = x = Test the function with values t = (0:2000)'/1000; f = 20.21; a = 1; p = (0:2000)'/1000; 20.21; 1; 0; sinsig(t,f,a,p);

%Q: Visualize the result by plotting. plot(t,x,'.-') title('Sinusoidal signal'); xlabel('Time') ylabel('Amplitude')

Using the function programmed generate a signal consisting of sum of n sinusoids.


n_arr = [1 2 5 10 20]; for ii = 1:numel(n_arr) n = n_arr(ii); f = 10 + 10*rand(n,1); a = 0.5 + rand(n,1); p = 2*pi*rand(n,1); x = zeros(size(t)); for jj = 1:n x = x+sinsig(t,f(jj),a(jj),p(jj)); end figure plot(t,x,'.-') title(sprintf('Sinusoidal signal with %d components', n)); xlabel('Time') ylabel('Amplitude') end

3. Try different visualizations


%Bar and stem plotfuns = {@bar @stem}; for kk = 1:numel(plotfuns) fun = plotfuns{kk}; for ii = 1:numel(n_arr) n = n_arr(ii); f = 10 + 10*rand(n,1); a = 0.5 + rand(n,1); p = 2*pi*rand(n,1); x = zeros(size(t)); for jj = 1:n x = x+sinsig(t,f(jj),a(jj),p(jj)); end figure fun(t,x) title(sprintf('Sinusoidal signal with %d components', n)); xlabel('Time') ylabel('Amplitude') end end %Symbols (you will need symbolic toolbox) syms xs ts for ii = 1:numel(n_arr)

n = n_arr(ii); f = 10 + 10*rand(n,1); a = 0.5 + rand(n,1); p = 2*pi*rand(n,1); syms xs; %Clear x for jj = 1:n xs = xs+sinsig(ts,f(jj),a(jj),p(jj)); end figure ezplot(xs, [0 2 -1*n 1*n]) %Plot signal from t = 0 to 2 and y axis from -1*n to 1*n title(sprintf('Sinusoidal signal with %d components', n)); xlabel('Time') ylabel('Amplitude') end

4. Estimate PDF of the signals (hist)


1. Single 2. Sum of 3. Sum of n_arr = [1 sinusoid two sinusoids several sinusoids 2 5 10 20];

for ii = 1:numel(n_arr) n = n_arr(ii); f = 10 + 10*rand(n,1); a = 0.5 + rand(n,1); p = 2*pi*rand(n,1); x = zeros(size(t)); for jj = 1:n x = x+sinsig(t,f(jj),a(jj),p(jj)); end figure hist(x,round(sqrt(numel(x)))); %Histogram with sqrt(numel(x)) bins title(sprintf('Histogram of sinusoidal signal with %d components', n)); xlabel('Bin centers') ylabel('Bin counts') end

5. Some extra exercises


%Q: * Try the effect of too low sampling frequency. %Q: * Try the effect of folding. figure samp_rate = 1; t = 0:samp_rate:10; f = 2.1; a = 1; p = 0; x = sinsig(t,f,a,p); plot(t,x,'.-') %Lets plot original signal t_org = 0:0.05:10; %Higher sampling frequence x_org = sinsig(t_org,f,a,p); hold on plot(t_org,x_org,'r-') hold off title('Sinusoidal signal (too low sampling frequancy causes folding)'); xlabel('Time') ylabel('Amplitude') legend('Sampled signal','Original signal') % * Generate an amplitude modulated signal by multiplying higher frequency sinusoid with lower frequency sinusoid. samp_rate = 0.05; t = 0:samp_rate:10;

f = 0.4; x = sinsig(t,f,a,p); f = 0.1; x = x .* sinsig(t,f,a,p); plot(t,x,'.-') title('Sinusoidal signal (amplitude modulation)'); xlabel('Time') ylabel('Amplitude')

6. Add to sinusoidal signal white noise generated by randn. Visualize the result with different signal to noise ratios S/N = [100 10 1 0.1]
samp_rate = 0.05; t = 0:samp_rate:10; f = 0.4; x = sinsig(t,f,a,p); SN_arr = [100 10 1 0.1]; for ii = 1:numel(SN_arr) SN = SN_arr(ii); xn = x+randn(size(x))*(1/SN); figure plot(t,xn,'.-') title(sprintf('Sinusoidal signal + noise. S/N = %f', SN)); xlabel('Time') ylabel('Amplitude') end

7. Create simple digital filter using Signal Processing Toolbox


Generate butterworth 2. order lowpass filter with the limit frequency 0.2*fs
doc butter doc filter %Generate signal when S/N is SN SN = 2; samp_rate = 0.05; t = 0:samp_rate:10; f = 0.4; x = sinsig(t,f,a,p); xn = x + randn(size(t))*(1/SN); plot(t,xn,'.-') %Visualize %Lets create digital butterworth filter order = 2; lim_f = 0.2; [fb fa] = butter(order,lim_f); %Now we can filter noisy signal xf = filter(fb,fa,xn); %Filter signal hold on plot(t,xf,'r-') hold off title(sprintf('Sinusoidal signal + noise. S/N = %f and filtered signal', SN)); xlabel('Time') ylabel('Amplitude') legend('Original signal','Filtered signal') Overloaded functions or methods (ones with the same name in other directories) <a href="matlab:doc signal/butter">doc signal/butter</a> Overloaded functions or methods (ones with the same name in other directories) <a href="matlab:doc comm/filter">doc comm/filter</a> <a href="matlab:doc filterdesign/filter">doc filterdesign/filter</a> <a href="matlab:doc finance/filter">doc finance/filter</a> <a href="matlab:doc signal/filter">doc signal/filter</a>

8. Use filtering to get better signal to noise ratio


%TODO: Onko oikein (tuskin) %Using knowladge of original signal (x) we can calculate new S/N %orig_SN = 1/std(xn-x) %new_SN = 1/std(xf-xn)

9. Do 7. and 8. using MATLAB Signal Procesing Toolbox GUI sptool.


sptool %1) Import noisy signal % -Click 'File'->'Import' % -Select 'xn' from list and click arrow ('-->') pointing to field 'Data' % -Select 'samp_rate' from list and add it to field 'Sampling frequency' % -Close dialog %2) Create new filter % -From SPTool main window click 'New' under 'Filters' section % -Select radio button 'IIR' from 'Design method' % -Make sure 'Butterworth' is selected at 'Design method' for IIR filter % -Set 'Specify order' under 'Filter order' to 2 % -Change frequancy units to 'Normalized (0 to 1)' under section 'Frequancy Specifications' % -Set 'wc' to 0.5 under 'Frequancy Specifications' % -Click 'Design filter' % -Close window %3) Use filter

% % % %

-Select both signal ('sig1') and filter ('filt1') from the lists -Click button 'Apply' under 'Filters' -Now window appears, click 'Ok' -Select new signal and click button 'View' under 'Signals'

Published with MATLAB 7.8

S-ar putea să vă placă și