-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp3b
54 lines (44 loc) · 3 KB
/
exp3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
% To verify the sampling theorem in frequency domain
%definiton of variables
%xanalog :Given analog signal%xsampled :Sampled version of xanalog
%xsampled_DFT :DFT of sampled signal %tsamp :sampling interval(spacing between any two samples)%xsampled_length :no. of samples of analog signal collected
%xreconstructed :sampled sequence in time domain
%fs:samplimg frequency(samples/second)
%t :time duration in which the analog signal exist (0 to 0.01(tfinal) in this program)
%fm :The highest frequency(700 Hz) contained in the analog signalsampling in time domain results in periodic components infrequency domain centered at nfs, where n=.... -3,-2,-1,1,2 %3,.....sampling in frequency domain can also be used to %analyze aliasing effect
tfinal=0.01;
t=0:0.00001:tfinal;
xanalog=cos(2*pi*400*t)+cos(2*pi*700*t);
%critical sampling fs=2fm(fm=700)
fs=1400;%Note that samples will repeat after 13/fs,hence its %sufficient if we consider first 14 samples(0 to 13) of analog %signal (observe the figures given previously)
tsamp=0:1/fs:13/fs;
xsampled=cos(2*pi*400*tsamp)+cos(2*pi*700*tsamp);%calculate 14 point DFT of sampled signal. abs(...) is %required because plot function displays only real values
xsampled_DFT=abs(fft(xsampled));
xsampled_length=0:length(xsampled_DFT)-1;
figure(1);subplot(4,1,1);stem(100*xsampled_length,xsampled_DFT);
xlabel('Frequency');ylabel('Magnitude');title('Critical Sampling (fs=2fm)');
xreconstructed=ifft(fft(xsampled)); %To reconstruct the signal, ifft(...) can be used.
subplot(4,1,2);plot(tsamp,xreconstructed,'b*-');
xlabel('Time');ylabel('Amplitude');title('Critical Sampling (fs=2fm)');
%Under Sampling(fs<2fm)
fs=700;%Note that samples will repeat after 6/fs,hence its sufficientif we consider first 7 samples(0 to 6) of analog signal
tsamp=0:1/fs:6/fs;
xsampled=cos(2*pi*400*tsamp)+cos(2*pi*700*tsamp);
xsampled_DFT=abs(fft(xsampled)); %calculate 7 point DFT of sampled signal.
xsampled_length=0:length(xsampled_DFT)-1;
subplot(4,1,3);stem(100*xsampled_length,xsampled_DFT)
xlabel('Frequency');ylabel('Magnitude');title('Under Sampling (fs<2fm)');
xreconstructed=ifft(fft(xsampled)); %To reconstruct the signal, ifft(...) can be used.
subplot(4,1,4);plot(tsamp,xreconstructed,'b*-');
xlabel('Time');ylabel('Amplitude');title('Under Sampling (fs<2fm)');
%over Sampling(fs>2fm)
fs=2000;%Note that samples will repeat after 19/fs,hence its %sufficient if we consider first 20 %samples(0 to 19) of analog %signal
tsamp=0:1/fs:19/fs;
xsampled=cos(2*pi*400*tsamp)+cos(2*pi*700*tsamp);%calculate 20 point DFT of sampled %signal.
xsampled_DFT=abs(fft(xsampled));
xsampled_length=0:length(xsampled_DFT)-1;
figure(2);subplot(4,1,1);stem(100*xsampled_length,xsampled_DFT)
xlabel('Frequency');ylabel('Magnitude');title('Over Sampling (fs>2fm)');
xreconstructed=ifft(fft(xsampled));
subplot(4,1,2);plot(tsamp,xreconstructed,'b*-');
xlabel('Time');ylabel('Amplitude');title('Over Sampling (fs>2fm)');