21EC10061 Sandeep Gr-06
21EC10061 Sandeep Gr-06
Experiment-1 (sampling)
Results:
N=64 points
clc;
clear all;
Fs = 12000;
t = 0: 1/Fs : 128/12000 -1/Fs;
x = 10*cos(2*pi*1000*t) + 6*cos(2*pi*2*1000*t) + 2*cos(2*pi*4*1000*t);
figure(1);
plot(t,x);
xlabel('time');
ylabel('Amplitude')
y = fftshift(fft(x));
f = (-length(y)/2 : length(y)/2 - 1) *Fs / length(y);
figure(2);
stem(f, abs(y)/128);
xlabel('frequency');
ylabel('|X(f)/128|')
clc;
clear all;
Fs = 12000;
t = 0: 1/Fs : 256/12000 -1/Fs;
x = 10*cos(2*pi*1000*t) + 6*cos(2*pi*2*1000*t) + 2*cos(2*pi*4*1000*t);
figure(1);
plot(t,x);
xlabel('time');
ylabel('Amplitude');
y = fftshift(fft(x));
f = (-length(y)/2 : length(y)/2 - 1) *Fs / length(y);
figure(2);
stem(f, abs(y)/256);
xlabel('frequency');
ylabel('|X(f)/256|');
Discussion: When we look at the frequency more times (N), the wave becomes easier to see
and has more details. It gets rid of sudden jumps and makes the shape of the wave
smoother. This is because we're looking at more spots on the wave, so the spaces between
them are smaller. This makes the picture on the graph more correct. We also found that
when we look at the frequency more times (N), the highest point of the wave on the graph
comes closer to the real frequency shown on the side. If we use N=64, we might see the
wrong shape of the wave because it shows a value even when it shouldn't. So, it's better to
use N=256 or more.
b) Repeat the a) part with Fs= 8KHz, 5KHz, 4KHz and observe the effect of sampling at critical,
and below a nyquist rate. Keeping N=64 points
Code:
clc;
clear all;
Fs = 4000;
t = 0: 1/Fs : 64/4000 -1/Fs;
x = 10*cos(2*pi*1000*t) + 6*cos(2*pi*2*1000*t) + 2*cos(2*pi*4*1000*t);
figure(1);
plot(t,x);
xlabel('time');
ylabel('Amplitude for Fs = 4KHz');
y = fftshift(fft(x));
f = (-length(y)/2 : length(y)/2 - 1) *Fs / length(y);
figure(2);
stem(f, abs(y)/64);
xlabel('frequency');
ylabel('|X(f)/64| for Fs = 4KHz');
clc;
clear all;
Fs = 5000;
t = 0: 1/Fs : 64/5000 -1/Fs;
x = 10*cos(2*pi*1000*t) + 6*cos(2*pi*2*1000*t) + 2*cos(2*pi*4*1000*t);
figure(1);
plot(t,x);
xlabel('time');
ylabel('Amplitude for Fs = 5KHz');
y = fftshift(fft(x));
f = (-length(y)/2 : length(y)/2 - 1) *Fs / length(y);
figure(2);
stem(f, abs(y)/64);
xlabel('frequency');
ylabel('|X(f)/64| for Fs = 5KHz');
clc;
clear all;
Fs = 8000;
t = 0: 1/Fs : 64/8000 -1/Fs;
x = 10*cos(2*pi*1000*t) + 6*cos(2*pi*2*1000*t) + 2*cos(2*pi*4*1000*t);
figure(1);
plot(t,x);
xlabel('time');
ylabel('Amplitude for Fs = 8KHz');
y = fftshift(fft(x));
f = (-length(y)/2 : length(y)/2 - 1) *Fs / length(y);
figure(2);
stem(f, abs(y)/64);
xlabel('frequency');
ylabel('|X(f)/64| for Fs = 8KHz');
Discussion: It's really important to make sure that when we take samples of a signal,
we do it often enough (sampling frequency) – at least twice for the highest
frequency. If we do exactly twice, things might look weird in our graphs, and it can be
hard to understand the signal. So, for better and clearer information, we need to do
it more than just twice.
If we don't follow this rule, we lose details about parts of the signal with frequencies
higher than half of how often we're sampling. This also affects how the signal looks
over time. It won't match the original signal. Also, there's something called aliasing.
Signals with one frequency might look like they have a different frequency because
of this. For example, if we take samples of a signal that goes up and down 4,000
times per second, but we only sample 4,000 times per second, it might seem like the
signal isn't moving at all (0 times per second). This can happen with other
frequencies too. If we sample at 5,000 times per second, a signal that goes up and
down 4,000 times per second might look like it's going up and down 1,000 times per
second (5,000 - 4,000 = 1,000 times per second).
Discussion: A square wave is made up of a main frequency and other frequencies that
are multiples of the main one, but these other frequencies get weaker as they go
higher. When we look at the square wave closely, it's not exactly straight up and
down in the time view. Making it perfectly straight would need a lot of different
frequencies, which isn't practical. But a close enough shape works fine for what we
need.
d) Interpolation or Up sampling
Input signal
Upsampled signal
Now we will pass it through different filters:
clc;
clear all;
Fs = 12000;
t = 0: 1/Fs : 256/12000 -1/Fs;
x = 2+3*sin(2*pi*10^3*t)+2*sin(2*pi*3*10^3*t);
% x = 1*cos(2*pi*6000*t);
figure(1);
plot(t,x);
xlabel('time');
ylabel('Amplitude for Fs = 12KHz');
y = fftshift(fft(x));
f = Fs/256*(-256/2:256/2-1);
figure(2);
stem(f, abs(y)/256);
xlabel('frequency');
ylabel('|X(f)/12| for Fs = 12KHz');
x_1 = upsample(x,2);
t_1 = 0: 1/(2*Fs) : 256/12000 -1/(2*Fs);
%t_1 = 0: 1/2*Fs : 256/24000 -1/2*Fs;
figure(3);
plot(t_1,x_1);
xlabel('Time(s)');
ylabel('Upsampled signal');
N = 256;
X = fft(x_1,N);
Y = fftshift(X);
f= 2*Fs/N * (-N/2:N/2-1);
figure(4);
stem(f,abs(Y)/N);
xlabel('Freq');
ylabel('|X_1(f)|/N');
title('upsamples signal spectrum');
fc = 6*10^3;
fs = 24*10^3;
[b,a] = butter(5,fc/(fs/2));
xf = filter(b,a,x_1);
figure(5);
subplot(121);
plot(t_1,xf);
xlabel('Time');
ylabel('amplitude');
title('Output passing after butter order 5');
N = 256;
X = fft(xf,N);
Y = fftshift(X);
[b,a] = cheby2(10,40,fc/(fs/2));
xf = filter(b,a,x_1);
figure(7);
plot(t_1,xf);
xlabel('Time');
ylabel('amplitude');
title('output after passing through cheby2 order 5 ripple 40');
N = 256;
X = fft(xf,N);
Y = fftshift(X);
f = 2*Fs/N*(-N/2:N/2-1);
figure(8);
stem(f,abs(Y)/N);
xlabel('freq');
ylabel('|X_f(f)|/N');
title('spectra of filtered signal cheby2');
[b,a] = ellip(10,2,40,fc/(fs/2));
xf = filter(b,a,x_1);
figure(9);
plot(t_1,xf);
xlabel('Time');
ylabel('amplitude');
title('output after passing through ellip order 5 ripplepass 2 ripplestop 50');
N = 256;
X = fft(xf, N);
Y = fftshift(X);
f = 2*Fs/N*(-N/2:N/2-1);
figure
stem(f, abs(Y)/N);
xlabel('Freq');
ylabel('|X_f(f)|/N');
title('Spectra of filtered signal ellip');
Discussions:
1) When we do up sampling, we add zeros in certain places to make the signal
longer. But this can make the signal look weird and different from before. The new
signal's frequency picture might have extra noise (unwanted parts) because of the
sudden changes we made. To fix this, we use some special filters that help make the
data look smoother.
2) Compared to the signal we had before, the signal we get after using these filters
looks much smoother. We found that as we change from one type of filter
(Butterworth) to another (Chebyshev), and then to another (Elliptic), the smoothing
gets better and better. But there's a trade-off: the better the smoothing, the more
the signal might take longer to show up, and there might be some changes to how it
looks.
3) We also learned that making the filter more complex (higher order, like from order
5 to order 10) makes the signal even smoother.