Skip to content

Instantly share code, notes, and snippets.

@kcs
Last active April 6, 2023 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcs/05d076ce9b26aa2da191 to your computer and use it in GitHub Desktop.
Save kcs/05d076ce9b26aa2da191 to your computer and use it in GitHub Desktop.
Exemple pentru laboratorul de procesări de semnale
% This file contains useful info and solutions for DSP lab exam
% some of the parameters need to be defined based on the actual problem
% sample rate
fs = 8000;
% duration
Td = 0.12;
% number of samples
N = Td * fs;
% time base
t = 0 : 1/fs : (N - 1) / fs;
% DTMF frequencies (based on the frequency table)
% following is symbol "1"
f1 = 1209;
f2 = 697;
% DTMF amplitude
Ad = 0.5
% DTMF tone
d = Ad * sin(2 * pi * f1 * t) + Ad * sin(2 * pi * f2 * t);
% overlayed signal
% for example triangle wave between 0 and 3V, rising 0.5ms, falling 1.5ms
% rising from
sl = 0;
% to
sh = 0.3;
% rise time
su = 0.5e-3;
% fall time
sd = 1.5e-3
% waveform
s = (sh + sl) / 2 * sawtooth(2 * pi * (1 / (su + sd)) * t, su / (su + sd)) + (sh - sl) / 2;
% other example square wave between having 0V for 0.5ms and 0.4V for 2ms
% high
sh = 0.4;
% high time
su = 2e-3;
% low
sl = 0;
% low time
sd = 0.5e-3
% waveform
s = (sh + sl) / 2 * square(2 * pi * (1 / (su + sd)) * t, su / (su + sd) * 100) + (sh - sl) / 2;
% combined signal
x = d + s;
% plotting the components
figure;
subplot(3,1,1), plot(t, d);
title("DTMF signal");
subplot(3,1,2), plot(t, s);
title("square wave");
subplot(3,1,3), plot(t, x);
title("combined signal");
% spectra
X = fft(x);
% plotting the spectra
w = 0 : 1 / Td : (N / 2 - 1) / Td;
figure;
plot(w, abs(X(1 : N / 2)));
title("spectra");
% frecvența de eșantionare 12kHz
fs = 12;
% baza de timp 0-160ms
t = 0:1/fs:160-1/fs;
% șir de biți
bits = [0, 1, 0, 0, 1, 1, 0, 1];
% interpolare șir de biți pe frecvența de eșantionare
sm = repmat(bits, 20 * fs, 1)(:)';
% modulare fsk+ask
s = 0.4 * sin(2*pi*0.1*t) .* (1 - sm) + 0.3 * sin(2*pi*0.33*t) .* sm;
%ph = (2 * pi * 0.1 * t) .* (1 - sm) + (2 * pi * 0.33 * t) .* sm;
%phc = cumsum((2*pi*0.1*t - 2*pi*0.33*t) .* (sm - [sm(1) sm(1:end-1)]));
%sm = movmean(sm, 2);
%s = 0.4 * sin(ph + phc) .* (1 - sm) + 0.3 * sin(ph + phc) .* sm;
% afișare trei grafice pe o figură
% controlul axelor pentru identificarea parametrilor
figure;
subplot(3,1,1);
plot(t,s);
title("Semnalul modulat")
xlabel("t[ms]");
ylabel("A[V]");
axis([0, 160, -0.5, 0.5]);
xticks(0:20:160);
yticks([-0.4, -0.3, 0.3, 0.4]);
grid on;
z = 0.1 * sawtooth(2*pi*0.2*t) + 0.1;
subplot(3,1,2);
plot(t,z);
title("Semnalul parazit");
xlabel("t[ms]");
ylabel("A[V]");
axis([0, 160, -0.1, 0.3]);
xticks(0:20:160);
yticks([0, 0.2]);
grid on
c = s + z;
subplot(3,1,3);
plot(t,c);
title("Semnalul combinat");
xlabel("t[ms]");
ylabel("A[V]");
axis([0, 160, -0.5 0.7]);
xticks(0:20:160);
yticks(-0.3:0.2:0.5);
% calculul spectrului
% se folosește întregul număr de eșantione
w = (0:1/160:fs/2-1/160) * 1000;
% și se afișează doar partea reală a spectrului
z = abs(fft(c))(1:160*fs/2)/(160*fs/2);
figure;
plot(w,z);
title("Spectrul semnalului");
xlabel("f[Hz]");
ylabel("Magnitudine");
% filtru trece bandă cu două benzi de trecere (pentru frecvențele fsk)
% doar filtrele FIR pot fi create cu mai multe benzi de trecere
% pentru a folosi IIR trebuie două filtre separate
h = fir1(250, [80/6000, 120/6000, 310/6000, 350/6000]);
f = filter(h,1,c);
figure;
subplot(2,1,1);
plot(t,c);
title("Semnalul original");
xlabel("t[ms]");
ylabel("A[V]");
axis([0, 160, -0.5, 0.7]);
xticks(0:20:160);
yticks(-0.3:0.2:0.5);
subplot(2,1,2);
plot(t,f);
title("Semnalul filtrat");
xlabel("t[ms]");
ylabel("A[V]");
axis([0, 160, -0.5, 0.5]);
xticks(0:20:160);
yticks(-0.4:0.2:0.4);
% baza de timp este dependent de frecvența de eșantionare fs și numărul dorit de eșantioane N
t = (0:N-1)/fs;
% construirea semnalelor parțiale
% a) din sinusoide
% cu parametrii: amplitudine (A), frecvență (f), defazaj (fi)
s1 = A1 * sin(2 * pi * f1 * t + fi1);
s2 = A2 * sin(2 * pi * f2 * t + fi2);
% etc ...
% b) semnal dreptunghiular
s3 = A3 * square(2 * pi * f3 * t + fi3);
% posibil offset (componentă continuă)
s4 = A4 * square(2 * pi * f4 * t + fi4) + Ofs;
% exemplu concret: oscilator TTL cu frecvența de bază fo
s5 = 2.5 * square(2 * pi * fo * t) + 2.5;
% semnale compus (combinații din semnale parțiale de mai sus)
s = s1 + s5;
% interpolarea semnalului cu un factor întreg (p)
sp = zeros(size(s) .* [1 p]);
sp(1:p:end) = s;
[b a] = cheby1(7, 0.5, 1/p);
sp = filter(b, a, sp);
% ex.: dublarea frecvenței de eșantionare
sd = zeros(size(s) .* [1 2]);
sd(1:2:end) = s;
[b a] = cheby1(7, 0.5, 1/2);
sd = filter(b, a, sd);
% decimarea semnalului cu factor întreg (q)
[b a] = cheby1(7, 0.5, 1/q);
sq = filter(b, a, s);
sq = sq(1:q:end);
% ex. înjumătățirea frecvenței de eșantionare
[b a] = cheby1(7, 0.5, 1/2);
sj = filter(b, a, s);
sj = sq(1:2:end);
% schimbarea ratei de eșantionare cu un factor rațional (p/q)
ss = zeros(size(s) .* [1 p]);
ss(1:p:end) = s;
[b a] = cheby1(7, 0.5, min(1/p, 1/q));
ss = filter(b, a, ss);
ss = ss(1:q:end);
% analiza spectrului unui semnal (s)
z = czt(s);
z = z / (length(z) / 2);
zm = abs(z);
zf = angle(z);
w = (0:fs*(N-1))/N;
subplot(2, 1, 1), plot(w, zm), subplot(2, 1, 2), plot(w, zf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment