0% found this document useful (0 votes)
10 views3 pages

Sampling

Uploaded by

Thekra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Sampling

Uploaded by

Thekra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

fs=500e3; %Very high sampling rate 500 kHz

f=10e3; %Frequency of sinusoid


nCyl=5; %generate five cycles of sinusoid
t=0:1/fs:nCyl*1/f; %time index
x=cos(2*pi*f*t);

plot(t,x)
title('Continuous sinusoidal signal');
xlabel('Time(s)');
ylabel('Amplitude');
%%
fs1=30e3; %30kHz sampling rate
t1=0:1/fs1:nCyl*1/f; %time index
x1=cos(2*pi*f*t1);

fs2=50e3; %50kHz sampling rate


t2=0:1/fs2:nCyl*1/f; %time index
x2=cos(2*pi*f*t2);

subplot(2,1,1);
plot(t,x);
hold on;
stem(t1,x1);
subplot(2,1,2);
plot(t,x);
hold on;
stem(t2,x2);
_________
how can i sample this code at fs=10000
my code
n = 0:35; a = 1.2; K =0.0017;
x = K*a.^n;
stem(n,x);
xlabel('Time index n');ylabel('Amplitude');
------
clear all; clc; close all;
a = 1.2; K =0.0017;
Fs = 10E3; % sampling frequency 10KHz
Ts = 1/Fs; % sampling period
tsamples = Ts*(0:35); % sampling instants
x = K*a.^tsamples;
%plot versus sample number
figure;
stem(x);
xlabel('index n');ylabel('Amplitude');
%plot versus time
figure;
stem(tsamples, x);
xlabel('time (sec)'); ylabel('Amplitude');
___________________-

Let’s generate a simple continuous-like sinusoidal signal with frequency f_m =


10\;kHz. In order to make it appear as a continuous signal when plotting, a
sampling rate of f_s=500\;kHz is used.

fs=500e3; %Very high sampling rate 500 kHz


f=10e3; %Frequency of sinusoid
nCyl=5; %generate five cycles of sinusoid
t=0:1/fs:nCyl*1/f; %time index
x=cos(2*pi*f*t);

plot(t,x)
title('Continuous sinusoidal signal');
xlabel('Time(s)');
ylabel('Amplitude');

Pretending the above generated signal as a “continuous” signal, we would like to


convert the signal to discrete-time equivalent by sampling. By Nyquist Shannon
Theorem, the signal has to be sampled at at-least f_s=2*f_m=20 kHz. Let’s sample
the signal at f_{s1}=30kHz and then at f_{s1}=50kHz for illustration.

fs1=30e3; %30kHz sampling rate


t1=0:1/fs1:nCyl*1/f; %time index
x1=cos(2*pi*f*t1);

fs2=50e3; %50kHz sampling rate


t2=0:1/fs2:nCyl*1/f; %time index
x2=cos(2*pi*f*t2);

subplot(2,1,1);
plot(t,x);
hold on;
stem(t1,x1);
subplot(2,1,2);
plot(t,x);
hold on;
stem(t2,x2);

_________________-
Manipulating audio files in Matlab
Matlab’s standard installation comes with a set of audio files. The audio
files,that can be considered as one-dimensional vectors, can be inspected and
played using xpsound command. With this command, we can visualize the audio files
in three ways

● Time series (data-vector as function of time)


● Power spectral density (distribution of frequency content)
● Spectrogram (frequency content as function of time)

The output of the xpsound command plotting time-series plot of a sample audio file
looks like this

We can also load and plot the time-series plot using inbuilt Matlab commands as
follows

>> load('gong') %load the variables for the 'gong' audio file, this loads the
sample frequency and the sample values
>> Fs %sampling frequency
Fs =

8192

>> y(1:10) %first 10 sample values in the file

ans =

-0.0027
-0.0045
-0.0074
-0.0110
-0.0128
-0.0173
-0.0223
-0.0223
-0.0200

-0.0092
>> t=0:1/Fs:length(y)/Fs-1/Fs; %time index
>> figure;plot(t,y);xlabel('Time (s)'),ylabel('y')

Example: DownSampling
In signal processing, downsampling is the process of throwing away samples without
applying any low-pass filtering. Mathematically, downsampling by a factor of M
implies, starting from the very first sample we throw away every $M-1$ samples
(i.e, keep every M-th sample.

https://www.gaussianwaves.com/2014/07/sampling-a-signal-in-matlab/

You might also like