This shows you the differences between two versions of the page.
| 
    ps:laboratoare:04 [2019/10/20 19:05] ionut.gorgos  | 
    
    ps:laboratoare:04 [2020/10/07 18:26] (current) ionut.gorgos  | 
    ||
|---|---|---|---|
| Line 68: | Line 68: | ||
| - scădeți pătratele termenilor de la $c_k, k \in \{-K...K\}$ obținând suma termenilor necesari pentru calcularea rms. | - scădeți pătratele termenilor de la $c_k, k \in \{-K...K\}$ obținând suma termenilor necesari pentru calcularea rms. | ||
| </note> | </note> | ||
| - | <hidden> | ||
| - | <note tip> | ||
| - | O posibilă soluție: | ||
| - | <code matlab ex1.m> | ||
| - | A = 1; | ||
| - | T = 100; | ||
| - | x = 1:T; | ||
| - | s = -A*ones(1, T); | ||
| - | s(1:(T/2)) = A; | ||
| - | |||
| - | h = figure; | ||
| - | plot(x,s); | ||
| - | ylabel('Amplitude'); | ||
| - | xlabel('Time'); | ||
| - | ylim([-A-0.5, A+0.5]); | ||
| - | title('Original signal s(t)'); | ||
| - | print(h, '-dpng', 'original_signal.png'); | ||
| - | |||
| - | % Compute some of the Fourier coefficients | ||
| - | cmax = 21; | ||
| - | kv = -cmax:cmax; | ||
| - | N = length(kv); | ||
| - | coef = zeros(N,1); | ||
| - | for i=1:N | ||
| - | k = kv(i); | ||
| - | if mod(k,2) ~= 0 | ||
| - | coef(i) = (2*A) / (j*pi*k); | ||
| - | end | ||
| - | end | ||
| - | |||
| - | % Plot power of coefficients | ||
| - | h = figure; | ||
| - | csquare = coef.*coef; | ||
| - | stem(kv,abs(csquare)); | ||
| - | xlabel('Frequency component (k)'); | ||
| - | ylabel('Magnitude of component'); | ||
| - | title('Fourier coefficients squared'); | ||
| - | print(h, '-dpng', 'squared_coefficients.png'); | ||
| - | |||
| - | |||
| - | % Plot rms of error signal | ||
| - | kmax = 500; | ||
| - | kv = 0:kmax; | ||
| - | N = length(kv); | ||
| - | coef = zeros(N,1); | ||
| - | for i=1:N | ||
| - | k = kv(i); | ||
| - | if mod(k,2) ~= 0 | ||
| - | coef(i) = (2*A) / (j*pi*k); | ||
| - | end | ||
| - | end | ||
| - | |||
| - | rmse = zeros(kmax, 1); | ||
| - | for i=1:kmax | ||
| - | ss = A^2 - 2 * coef(2:i)'*coef(2:i) - coef(1)^2; | ||
| - | rmse(i) = sqrt(ss); | ||
| - | end | ||
| - | |||
| - | h=figure; | ||
| - | plot(0:(kmax-1), rmse); % Look with plot, semilogy and loglog | ||
| - | title('RMS of e_k'); | ||
| - | ylabel('RMS(e_k)'); | ||
| - | xlabel('K'); | ||
| - | grid on; | ||
| - | print(h, '-dpng', 'rms_ek.png'); | ||
| - | |||
| - | % Reconstruct signal with only some of the components | ||
| - | % Note: need to use both positive and negative k's | ||
| - | % The fourier coeficients  c(-k) = conj(c(k)), for real signals | ||
| - | % As the original signal is odd, the fourier coeficient are completly imaginary | ||
| - | % so c(-k) = -c(k) | ||
| - | |||
| - | fk = find(rmse < 0.05); | ||
| - | N = fk(1); | ||
| - | sr = zeros(1, T); | ||
| - | for t=1:T | ||
| - | for i=1:N | ||
| - | sr(t) = sr(t) + coef(i)*exp(j*2*pi*kv(i)*t/T) ... | ||
| - | - coef(i)*exp(-j*2*pi*kv(i)*t/T); | ||
| - | end | ||
| - | end | ||
| - | |||
| - | h=figure; | ||
| - | plot(1:T, sr); | ||
| - | ylim([-A-1, A+1]); | ||
| - | title('Reconstructed signal s(t)'); | ||
| - | ylabel('Amplitude'); | ||
| - | xlabel('Time'); | ||
| - | print(h, '-dpng', 'reconstructed_signal.png'); | ||
| - | </code> | ||
| - | </note> | ||
| - | </hidden> | ||
| - | |||
| === Exercițiul 2 - filtrare [4p] === | === Exercițiul 2 - filtrare [4p] === | ||
| Line 205: | Line 112: | ||
| - $f_c = 10/T$ (frecvența de cut-off mult mai mare decât frecvența fundamentală => filtrare slabă) | - $f_c = 10/T$ (frecvența de cut-off mult mai mare decât frecvența fundamentală => filtrare slabă) | ||
| - Reconstruiți semnalul de output cu ajutorul seriei Fourier (folosind formula de la exercițiul 1) [1p] | - Reconstruiți semnalul de output cu ajutorul seriei Fourier (folosind formula de la exercițiul 1) [1p] | ||
| - | |||
| - | <hidden> | ||
| - | <note tip> | ||
| - | Ideea este să folosiți seriile Fourier pentru a determina ieșirea filtrului linear. | ||
| - | <code matlab ex3.m> | ||
| - | A = 1; | ||
| - | D = 20; | ||
| - | T = 100; | ||
| - | x = 1:T; | ||
| - | s = zeros(1,T); | ||
| - | s(1:D) = A; | ||
| - | |||
| - | h = figure; | ||
| - | plot(x,s); | ||
| - | ylabel('Amplitude'); | ||
| - | xlabel('Time'); | ||
| - | ylim([-A-0.5, A+0.5]); | ||
| - | title('Original signal s(t)'); | ||
| - | print(h, '-dpng', 'pulse_signal.png'); | ||
| - | |||
| - | % Compute some of the Fourier coefficients | ||
| - | cmax = 20; | ||
| - | kv = 0:cmax; | ||
| - | N = length(kv); | ||
| - | coef = zeros(N,1); | ||
| - | for i=1:N | ||
| - | k = kv(i); | ||
| - | coef(i) = (D/T)*A*exp(-j*pi*k*D/T)*sinc(k*D/T); | ||
| - | end | ||
| - | |||
| - | % Plot magnitude of pulse's coefficients | ||
| - | h = figure; | ||
| - | stem(kv, abs(coef)); | ||
| - | xlabel('Frequency component (k)'); | ||
| - | ylabel('Magnitude of component'); | ||
| - | title('Input Fourier coefficients'); | ||
| - | hold on; | ||
| - | plot(kv, abs(coef), 'r--'); | ||
| - | print(h, '-dpng', 'coef_pulse.png'); | ||
| - | |||
| - | % Compute coefficients of output after lp filter | ||
| - | fcoef = zeros(N,1); | ||
| - | %fc = 0.1/T; | ||
| - | %fc = 1/T; | ||
| - | fc = 10/T; | ||
| - | RC = 1/(2*pi*fc); | ||
| - | for i=1:N | ||
| - | k = kv(i); | ||
| - | fcoef(i) = coef(i) / (1+j*2*pi*RC*k/T); | ||
| - | end | ||
| - | |||
| - | % Plot magnitude of output's coefficients | ||
| - | h = figure; | ||
| - | stem(kv, abs(fcoef)); | ||
| - | xlabel('Frequency component (k)'); | ||
| - | ylabel('Magnitude of component'); | ||
| - | title('Output Fourier coefficients'); | ||
| - | hold on; | ||
| - | plot(kv, abs(fcoef), 'r--'); | ||
| - | print(h, '-dpng', 'coef_out.png'); | ||
| - | |||
| - | %Construct output signal with the computed coefficients | ||
| - | sr = zeros(1, T); | ||
| - | for t=1:T | ||
| - | for i=1:N | ||
| - | if kv(i) ~= 0 | ||
| - | sr(t) = sr(t) + fcoef(i)*exp(j*2*pi*kv(i)*t/T) ... | ||
| - | + conj(fcoef(i))*exp(-j*2*pi*kv(i)*t/T); | ||
| - | else | ||
| - | sr(t) = sr(t) + fcoef(i)*exp(j*2*pi*kv(i)*t/T); | ||
| - | end | ||
| - | end | ||
| - | end | ||
| - | |||
| - | h=figure; | ||
| - | plot(1:T, sr); | ||
| - | ylim([-A, A]); | ||
| - | title('Output filtered signal s(t)'); | ||
| - | ylabel('Amplitude'); | ||
| - | xlabel('Time'); | ||
| - | print(h, '-dpng', 'output_signal.png'); | ||
| - | </code> | ||
| - | <note> | ||
| - | If time allows and some of the students are interested, it would be nice to make a SPICE simulation | ||
| - | (e.g. on ngspice.com) with a pulse signal and an RC low-pass filter to check that the output | ||
| - | of the simulation is similar to what we did in MATLAB. | ||
| - | |||
| - | Here is an example of what to write in ngspice to get the "shark teeth" (for fc=ft): | ||
| - | <code> | ||
| - | *RC Filter Transient example | ||
| - | |||
| - | V1 in 0 PULSE(0 1 1ns 1ns 1ns 40us 200us) | ||
| - | R1 in out 3.2k | ||
| - | C1 out 0 10n | ||
| - | |||
| - | .TRAN 10n 1m | ||
| - | .end | ||
| - | </code> | ||
| - | Change the capacitor value to modify the effect of the filter (smaller values mean a weaker filtering, while larger values mean a stronger filtering). | ||
| - | </note> | ||
| - | |||
| - | </note> | ||
| - | </hidden> | ||
| - | |||
| === Exercițiul 3 - comunicație digitală [Bonus] === | === Exercițiul 3 - comunicație digitală [Bonus] === | ||
| Line 325: | Line 128: | ||
| Notă: pentru a genera o secvență random de valori întregi inspectați funcția 'randi' din MATLAB. | Notă: pentru a genera o secvență random de valori întregi inspectați funcția 'randi' din MATLAB. | ||
| - | <hidden> | ||
| - | <note tip> | ||
| - | Posibila solutie: | ||
| - | <code matlab> | ||
| - | %% Frquency modulation using two frequencies | ||
| - | close all; | ||
| - | k1 = 1; | ||
| - | k2 = 2; | ||
| - | T = 1; | ||
| - | f1 = k1/T; | ||
| - | f2 = k2/T; | ||
| - | t = 0:T/100:T; | ||
| - | s1 = sin(2*pi*f1*t); | ||
| - | s2 = sin(2*pi*f2*t); | ||
| - | values = randi([0 3], 1, 10); | ||
| - | nv = length(values); | ||
| - | s = []; | ||
| - | for i=1:nv | ||
| - | si = zeros(1,length(s1)); | ||
| - | if mod(values(i), 2) == 1 | ||
| - | si = si + s1; | ||
| - | end | ||
| - | if values(i) > 1 | ||
| - | si = si + s2; | ||
| - | end | ||
| - | s = [s, si]; | ||
| - | end | ||
| - | tt = length(s); | ||
| - | x = ((1:tt) - 1) * nv / tt; | ||
| - | h1 = figure; | ||
| - | plot(x, s); | ||
| - | grid on; | ||
| - | xlabel('Time (s)'); | ||
| - | ylabel('Amplitude'); | ||
| - | title('Frequency modulated signal'); | ||
| - | print(h1, '-dpng', 'freqmod.png'); | ||
| - | fprintf('The transmitted values are:\n'); | ||
| - | values | ||
| - | </code> | ||
| - | </note> | ||
| - | </hidden> | ||
| /*</hidden>*/ | /*</hidden>*/ | ||