What Is The Objective Of This Topic:
The objective of this topic is to perform sampling on audio signals.
What Is The Description Of This Topic:
In signal processing, sampling is the reduction of a continuous signal to a discrete signal. A common example is the conversion of a sound wave (a continuous signal) to a sequence of samples (a discrete-time signal).
A sample is a value or set of values at a point in time and/or space. While a sampler is a subsystem or operation that extracts samples from a continuous signal.
A theoretical ideal sampler produces samples equivalent to the instantaneous value of the continuous signal at the desired points.
The Nyquist sampling theorem provides a prescription for the nominal sampling interval required to avoid aliasing. It may be stated simply as follows:
The sampling frequency should be at least twice the highest frequency contained in the signal.
Fs >=2Fc
where fs is the sampling frequency (how often samples are taken per unit of time or space), and fc is the highest frequency contained in the signal. That this is so is really quite intuitive. Consider for example a signal composed of a single sinewave at a frequency of 1 Hz:
If we sample this waveform at 2 Hz (as dictated by the Nyquist theorem), that is sufficient to capture each peak and trough of the signal:
If we sample at a frequency higher than this, for example 3 Hz, then there are more than enough samples to capture the variations in the signal:
If, however we sample at a frequency lower than 2 Hz, for example at 1.5 Hz, then there are now not enough samples to capture all the peaks and troughs in the signal:
Note here that we are not only losing information, but we are getting the wrong information about the signal. The person receiving these samples, without any previous knowledge of the original signal, may well be mislead into thinking that the signal has quite a different form:
From this example, we can see the reason for the term aliasing. That is, the signal now takes on a different \persona," or a false presentation, due to being sampled at an insufficiently high frequency. Now we are ready to think about the sampling of a complex signal composed of many frequency components. By Fourier's theorem, we know that any continuous signal may be decomposed in terms of a sum of sines and cosines at different frequencies.
For example, the following waveform was composed by adding together sine waves at frequencies of 1 Hz, 2 Hz, and 3 Hz:
According to the Nyquist sampling theorem, the signal must be sampled at twice the highest frequency contained in the signal. In this case, we have fc=3 Hz, and so the Nyquist theorem tells us that the sampling frequency, fs, must be at least 6 Hz. And sure enough, this appears to be sufficient:
Here are some of the practical examples we will try to solve them using MATLAB:
001: Prove Nyquist’s Sampling Theorem, by sampling the following waves
a) Y = Cos(2*pi*f*t)
Where f=5Hz
b) Y = Sin(2*pi*f1*t) + Cos(2*pi*f2*t)
Where f1 = 100Hz and f2 = 400Hz
at three possible sampling rates i.e.
(1) Fs =2Fc (2) Fs <2Fc (3) Fs >2Fc
Part(1)
clear all;
f = 5 ;
%frequency
of cosine wave
Fs1 = 5000 ;
%sampling
frequency of original signal
dt = 1/Fs1;
%seconds
per sample
Time = 1;
%End time
t1 = (0:dt:Time)';
%range of
time axis
x1 = cos(2*pi*f*t1);
%cosine
signal
Fs2 = 10 ;
%sampling
frequency Fs = 2*fc
dt = 1/Fs2;
%seconds
per sample
Time = 1;
%End time
t2 = (0:dt:Time)';
%range of
time axis
x2 = cos(2*pi*f*t2);
%cosine
signal
Fs3 = 20 ;
%sampling
frequency Fs > 2*fc
dt = 1/Fs3;
%seconds
per sample
Time = 1;
%End time
t3 = (0:dt:Time)';
%range of
time axis
x3 = cos(2*pi*f*t3);
%cosine
signal
Fs4 = 5 ;
%sampling
frequency Fs < 2*fc
dt = 1/Fs4;
%seconds
per sample
Time = 1;
%End time
t4 = (0:dt:Time)';
%range of
time axis
x4 = cos(2*pi*f*t4);
%cosine
signal
subplot(2,2,1)
%Plot
Original Signal
plot (t1,x1)
xlabel('Time (sec)')
ylabel('Amplitude')
title('Original
Signal of fc 5 Hz and Fs 5000 Hz')
subplot(2,2,2)
%Plot
Sampled Signal with Fs=2*fc
plot (t1,x1)
hold on
stem (t2,x2)
hold off
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled
Signal at Fs 10 Hz (Fs=2*fc)')
subplot(2,2,3)
%Plot
Sampled Signal with Fs>2*fc
plot (t1,x1)
hold on
stem (t3,x3)
hold off
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled
Signal at Fs 20 Hz (Fs>2*fc)')
subplot(2,2,4)
%Plot
Sampled Signal with Fs<2*fc
plot (t1,x1)
hold on
stem (t4,x4)
hold off
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled
Signal at Fs 5 Hz (Fs<2*fc)')
OUTPUT:
Part (2)
CODE:
clear all;
f1 = 100 ; %frequency
of sine wave
f2 = 400 ; %frequency
of cosine wave
Fs1 = 4550 ; %sampling
frequency of original signals
dt1 = 1/Fs1; %seconds
per sample
Time = 0.05; %End time
t1 = (0:dt1:Time)'; %range of time axis
x1 = sin(2*pi*f1*t1); %sine signal
x2 = cos(2*pi*f2*t1); %cosine signal
y1 = x1+x2; %input
signal
% Max frequency is the max
of the 2 summing signals
% So fc = 400 Hz and Fs =
800 Hz according to Nyquist
Fs2 = 800 ; %sampling
frequency Fs = 2*fc
dt2 = 1/Fs2; %seconds
per sample
Time = 0.02; %End time
t2 = (0:dt2:Time)'; %range of time axis
x1a = sin(2*pi*f1*t2); %sine signal
x2a = cos(2*pi*f2*t2); %cosine signal
y2 = x1a+x2a; %Sampled
Signal at Fs = 2*fc
Fs3 = 1000 ; %sampling
frequency Fs > 2*fc
dt3 = 1/Fs3; %seconds
per sample
Time = 0.02; %End time
t3 = (0:dt3:Time)'; %range of time axis
x1b = sin(2*pi*f1*t3); %sine signal
x2b = cos(2*pi*f2*t3); %cosine signal
y3 = x1b+x2b; %Sampled
Signal at Fs > 2*fc
Fs4 = 400 ; %sampling
frequency Fs < 2*fc
dt4 = 1/Fs4; %seconds
per sample
Time = 0.02; %End time
t4 = (0:dt4:Time)'; %range of time axis
x1c = sin(2*pi*f1*t4); %sine signal
x2c = cos(2*pi*f2*t4); %cosine signal
y4 = x1c+x2c; %Sampled
Signal at Fs < 2*fc
subplot(3,3,1) %Plot
Original Sine Signal
plot (t1,x1)
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sinusoidal
Signal of fc 100 Hz')
subplot(3,3,2) %Plot Original
Sine Signal
plot (t1,x2)
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sinusoidal
Signal of fc 400 Hz')
subplot(3,3,3) %Plot
Sampled Signal with Fs>2*fc
plot (t1,y1)
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sum of Sinusoidal
Signals of fc 100 & 400 Hz')
%Updating time axis for
Input Signal for better visualization
Time = 0.02; %End time
t1 = (0:dt1:Time)'; %range of time axis
x1 = sin(2*pi*f1*t1); %sine signal
x2 = cos(2*pi*f2*t1); %cosine signal
y1 = x1+x2; %Input
signal with updated time axis
subplot(3,3,4) %Plot
Sampled Signal with Fs=2*fc
plot (t1,y1)
hold on
stem (t2,y2)
hold off
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled
Signal at 800 Hz (Fs=2*fc)')
subplot(3,3,5) %Plot
Sampled Signal with Fs>2*fc
plot (t1,y1)
hold on
stem (t3,y3)
hold off
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled
Signal at 1000 Hz (Fs>2*fc)')
subplot(3,3,6) %Plot
Sampled Signal with Fs<2*fc
plot (t1,y1)
hold on
stem (t4,y4)
hold off
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled
Signal at 400 Hz (Fs<2*fc)')
OUTPUT:
02: Sample an audio with Fs=2Fc, Fs < 2Fc and Fs >2Fc. Observe the effect of all cases.
CODE:
clc
clear all;
[y,Fs] = audioread('Bach2.wav'); %read
input audio file
samples = [1,5*Fs]; %extract 5
seconds
clear y Fs
[y,Fs] = audioread('Bach2.wav',samples); %updated Sampling Frequency and
Sampled data y
input = y(:,1); %extract one channel
Fourier = abs(fft(input)); %compute fourier
[Magnitude,Fmax] = max(Fourier); %find
maximum frequency
fy =
(0:length(Fourier)-1)*Fs/length(Fourier); %axis for Fourier Transform
info = audioinfo('Bach2.wav'); %info about audio file
Fs1 = Fmax*2; %Fs = 2*fc
Fs2 = Fs1 + 1000 ; %Fs > 2*fc
Fs3 = Fs1 - 1000 ; %Fs <
2*fc
audiowrite('C:\Desktop\Double.wav',input,Fs1); %writing audio with Fs = 2*fc
[p,Fs1] = audioread('C:\Desktop\Double.wav'); %reading audio with Fs = 2*fc
audiowrite('C:\Desktop\High.wav',input,Fs2); %writing audio with Fs > 2*fc
[p2,Fs2] = audioread('C:\Desktop\High.wav'); %reading audio with Fs >
2*fc
audiowrite('C:\Desktop\Low.wav',input,Fs3); %writing audio with Fs < 2*fc
[p3,Fs3] = audioread('C:\Desktop\Low.wav'); %reading audio with Fs < 2*fc
%time axis for original
signal
T = length(input);
dt = 1/Fs;
t = 0:dt:(T*dt)-dt;
%time axis for signal with
Fs = 2*fc
T1 = length(p);
dt1 = 1/Fs1;
t1 = 0:dt1:(T1*dt1)-dt1;
%time axis for signal with
Fs > 2*fc
T2 = length(p2);
dt2 = 1/Fs2;
t2 = 0:dt2:(T2*dt2)-dt2;
%time axis for signal with
Fs < 2*fc
T3 = length(p3);
dt3 = 1/Fs3;
t3 = 0:dt3:(T3*dt3)-dt3;
figure
subplot(3,1,1) %Plot
Original Audio Signal
plot (t,input)
xlabel('Time (sec)')
ylabel('Amplitude')
title('Original
Audio Signal ')
subplot(3,1,2)
plot (fy,Fourier)
xlabel('Frequency
(Hz)')
ylabel('Magnitude')
title('Fourier
Transform of Original Audio Signal ')
subplot(3,1,3) %Plot
Sampled Audio Signal with Fs<2*fc
plot (t,input)
hold on
stem (t,input)
hold off
xlim([4.5025 4.505])
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled Audio
Signal at original Fs')
figure
subplot(3,1,1) %Plot
Sampled Audio Signal with Fs=2*fc
plot (t,input)
hold on
stem (t1,p)
hold off
xlim([4.5025 4.505])
% xlim([0.8 0.81])
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled Audio
Signal (Fs=2*fc)')
subplot(3,1,2) %Plot
Sampled Audio Signal with Fs>2*fc
plot (t,input)
hold on
stem (t2,p2)
hold off
xlim([4.5025 4.505])
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled Audio
Signal (Fs>2*fc)')
subplot(3,1,3) %Plot
Sampled Audio Signal with Fs<2*fc
plot (t,input)
hold on
stem (t3,p3)
hold off
xlim([4.5025 4.505])
xlabel('Time (sec)')
ylabel('Amplitude')
title('Sampled Audio
Signal (Fs<2*fc)')
OUTPUT:
No comments:
Post a Comment