
clc
clear all
close all
%%
%%%%%%%%%%%%%%%%%%%%%%%--1--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f1 = 50; % first frequency
f2 = 100; % second frequency
p_N = 3; % number of periods
fs = 100*20; % Sampling frequency (Hz)
duration = 1/f1*p_N; % signal duration (second)
N = fs*duration; % total number of samples
t = 0:1/fs:duration-1/fs; % time vector
s1 = cos(2*pi*f1*t);
s2 = cos(2*pi*f2*t);
% finding FFT
S1 = fft(s1);
S2 = fft(s2);
% Normalizing and cleaning spectrum
S1_oneSide = S1(1:N/2);
S2_oneSide = S2(1:N/2);
f = fs*(0:N/2 - 1)/N;
S1_meg = abs(S1_oneSide)/(N/2);
S2_meg = abs(S2_oneSide)/(N/2);
% Let's plot it now
figure
subplot(2, 1, 1)
plot(t, s1, 'green')
xlabel('Time (sec)')
ylabel('f(t)')
subplot(2, 1, 2);
bar(f, S1_meg)
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Spectrum')
figure
subplot(2, 1, 1)
plot(t, s2, 'red')
xlabel('Time (sec)')
ylabel('f(t)')
subplot(2, 1, 2);
bar(f, S2_meg)
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Spectrum')
%%
%%%%%%%%%%%%%%%%%%%%%%%--2--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s3 = s1 + s2;
s4 = s1.*s2;
% finding FFT
S3 = fft(s3);
S4 = fft(s4);
% Normalizing and cleaning spectrum
S3_oneSide = S3(1:N/2);
S4_oneSide = S4(1:N/2);
f = fs*(0:N/2 - 1)/N;
S3_meg = abs(S3_oneSide)/(N/2);
S4_meg = abs(S4_oneSide)/(N/2);
% Let's plot it now
figure
subplot(211);
plot(t, s3, 'red')
xlabel('Time (sec)')
ylabel('f(t)')
subplot(212);
bar(f, S3_meg)
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Spectrum')
subplot(211);
figure
subplot(211);
plot(t, s4, 'red')
xlabel('Time (sec)')
ylabel('f(t)')
subplot(212);
bar(f, S4_meg)
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Spectrum')
% The result is clear: when we summed our signals,
% we got a number of simple spectrum.
% When we multiplied them, we got
% sin(w2*t)*sin(w1*t) = 0.5*cos((w1 - w2)t) - 0.5*cos((w1 + w2)t)
% So f1_new = abs(100-50) = 50, f2_new = abs(100+50) = 150.
%%
%%%%%%%%%%%%%%%%%%%%%%%--3--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N_Delta = 2^7;
t_delta = 0:1/(N_Delta):1 - 1/(N_Delta);
delta = zeros(1, N_Delta);
delta(N_Delta/2 + 1) = 1;
figure
subplot(311);
bar(t_delta, delta, 'green')
xlabel('Time (sec)')
ylabel('f(t)')
ylim([-0.1, 1.1]);
grid on
% plot(t_delta, delta);
% I’m pretty sure you know what the Dirac function looks like
% Let's calculate some fu**ing weird stuff
% plot(t_delta, delta);
% I’m pretty sure you know what the Dirac function looks like
% Let's calculate some fu**ing weird stuff
Delta = fft(delta);
subplot(312)
plot(t_delta, abs(Delta), 'LineWidth', 1);
ylim([-0.1, 1.1]);
grid on
title('S');
subplot(313)
plot(t_delta, angle(Delta), 'LineWidth', 1, 'Color', 'red');
ylim([-0.1, pi+0.1]);
grid on
title('P');
% Changing the pulse time does not change the amplitude spectrum
% but phase spectrum
%%
%%%%%%%%%%%%%%%%%%%%%%%--4--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=2^7; % power of points
T=1; % impulse duration
dt=T/(N);
Nyq=N/(2*T); % nyquist frequency
df=1/T;
t=linspace(0,T,N);
for i = (1:8)
figure
T_h = i*2^-6; % impuls hight duration
s = zeros(1, N);
s(N/2 + 1 - (T_h/dt)/2 : N/2 + (T_h/dt)/2) = 1;
F=fft(s)/N; % FFT
%subplot(411);
subplot(211);
bar(t,s, 'green');%
ylim([-0.1, 1.1])
grid on
title('Impulse')
F1=F(1:N/2+1); % positive frequencies
F2=F(N/2+1:N); % negative frequencies
F=[F2,F1]; % full
nu=-Nyq+df*(0:N);
subplot(212)
%subplot(412);
%plot(nu(1:N),real(F(1:N)))
%subplot(413);
%plot(nu(1:N),imag(F(1:N)))
%subplot(414);
plot(nu(1:N),abs(F(1:N)),'LineWidth', 1)
grid on
title('Spectrum')
end
%%
%%%%%%%%%%%%%%%%%%%%%%%--5--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N_p = 8; % # of impulses
N = 2^7; % # of points
T=1/N_p; % impulse duration
dt=T/(N);
Nyq=N/(2*T); % nyquist frequency
df=1/T;
t = 0:2*pi/N:2*pi - 2*pi/N;
r = 0.5*square(N_p*t, 50) + 0.5; % creating a square wave with 50 duty cycle
figure
subplot(211)
bar(t/pi, r, 'green')
ylim([-0.1, 1.1]);
grid on
title('Señal')
R = fft(r)/N;
R1=R(1:N/2+1); % positive frequencies
R2=R(N/2+1:N); % negative frequencies
R=[R2,R1]; % full
nu=(-Nyq+df*(0:N))*2*pi/(N_p); % freq. vectror with normalisation
subplot(212)
bar(nu(1:N),abs(R(1:N)), 'blue')
grid on
title('Espectro');
ylim([-0.01, 0.6]);
%%
%%%%%%%%%%%%%%%%%%%%%%%--6--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
H_1 = [1, 1; 1, -1];
H = H_1;
for i = (1:5)
H = kron(H_1, H);
end
figure
subplot(211)
bar(t/pi, r, 'green')
ylim([-0.1, 1.1]);
grid on
R_H = fwht(r); % try fast Walsh-Hadamard transform
subplot(212)
%fw = 1:N;
bar(R_H, 'blue')
ylim([-0.01, 0.51]);
grid on
%%
%%%%%%%%%%%%%%%%%%%%%%%--7--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%%%%%%%%%%%%%%%%%%%%%%--8--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear all
close all
%%
%%%%%%%%%%%%%%%%%%%%%%%--1--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f1 = 50; % first frequency
f2 = 100; % second frequency
p_N = 3; % number of periods
fs = 100*20; % Sampling frequency (Hz)
duration = 1/f1*p_N; % signal duration (second)
N = fs*duration; % total number of samples
t = 0:1/fs:duration-1/fs; % time vector
s1 = cos(2*pi*f1*t);
s2 = cos(2*pi*f2*t);
% finding FFT
S1 = fft(s1);
S2 = fft(s2);
% Normalizing and cleaning spectrum
S1_oneSide = S1(1:N/2);
S2_oneSide = S2(1:N/2);
f = fs*(0:N/2 - 1)/N;
S1_meg = abs(S1_oneSide)/(N/2);
S2_meg = abs(S2_oneSide)/(N/2);
% Let's plot it now
figure
subplot(2, 1, 1)
plot(t, s1, 'green')
xlabel('Time (sec)')
ylabel('f(t)')
subplot(2, 1, 2);
bar(f, S1_meg)
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Spectrum')
figure
subplot(2, 1, 1)
plot(t, s2, 'red')
xlabel('Time (sec)')
ylabel('f(t)')
subplot(2, 1, 2);
bar(f, S2_meg)
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Spectrum')
%%
%%%%%%%%%%%%%%%%%%%%%%%--2--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s3 = s1 + s2;
s4 = s1.*s2;
% finding FFT
S3 = fft(s3);
S4 = fft(s4);
% Normalizing and cleaning spectrum
S3_oneSide = S3(1:N/2);
S4_oneSide = S4(1:N/2);
f = fs*(0:N/2 - 1)/N;
S3_meg = abs(S3_oneSide)/(N/2);
S4_meg = abs(S4_oneSide)/(N/2);
% Let's plot it now
figure
subplot(211);
plot(t, s3, 'red')
xlabel('Time (sec)')
ylabel('f(t)')
subplot(212);
bar(f, S3_meg)
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Spectrum')
subplot(211);
figure
subplot(211);
plot(t, s4, 'red')
xlabel('Time (sec)')
ylabel('f(t)')
subplot(212);
bar(f, S4_meg)
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Spectrum')
% The result is clear: when we summed our signals,
% we got a number of simple spectrum.
% When we multiplied them, we got
% sin(w2*t)*sin(w1*t) = 0.5*cos((w1 - w2)t) - 0.5*cos((w1 + w2)t)
% So f1_new = abs(100-50) = 50, f2_new = abs(100+50) = 150.
%%
%%%%%%%%%%%%%%%%%%%%%%%--3--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N_Delta = 2^7;
t_delta = 0:1/(N_Delta):1 - 1/(N_Delta);
delta = zeros(1, N_Delta);
delta(N_Delta/2 + 1) = 1;
figure
subplot(311);
bar(t_delta, delta, 'green')
xlabel('Time (sec)')
ylabel('f(t)')
ylim([-0.1, 1.1]);
grid on
% plot(t_delta, delta);
% I’m pretty sure you know what the Dirac function looks like
% Let's calculate some fu**ing weird stuff
% plot(t_delta, delta);
% I’m pretty sure you know what the Dirac function looks like
% Let's calculate some fu**ing weird stuff
Delta = fft(delta);
subplot(312)
plot(t_delta, abs(Delta), 'LineWidth', 1);
ylim([-0.1, 1.1]);
grid on
title('S');
subplot(313)
plot(t_delta, angle(Delta), 'LineWidth', 1, 'Color', 'red');
ylim([-0.1, pi+0.1]);
grid on
title('P');
% Changing the pulse time does not change the amplitude spectrum
% but phase spectrum
%%
%%%%%%%%%%%%%%%%%%%%%%%--4--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=2^7; % power of points
T=1; % impulse duration
dt=T/(N);
Nyq=N/(2*T); % nyquist frequency
df=1/T;
t=linspace(0,T,N);
for i = (1:8)
figure
T_h = i*2^-6; % impuls hight duration
s = zeros(1, N);
s(N/2 + 1 - (T_h/dt)/2 : N/2 + (T_h/dt)/2) = 1;
F=fft(s)/N; % FFT
%subplot(411);
subplot(211);
bar(t,s, 'green');%
ylim([-0.1, 1.1])
grid on
title('Impulse')
F1=F(1:N/2+1); % positive frequencies
F2=F(N/2+1:N); % negative frequencies
F=[F2,F1]; % full
nu=-Nyq+df*(0:N);
subplot(212)
%subplot(412);
%plot(nu(1:N),real(F(1:N)))
%subplot(413);
%plot(nu(1:N),imag(F(1:N)))
%subplot(414);
plot(nu(1:N),abs(F(1:N)),'LineWidth', 1)
grid on
title('Spectrum')
end
%%
%%%%%%%%%%%%%%%%%%%%%%%--5--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N_p = 8; % # of impulses
N = 2^7; % # of points
T=1/N_p; % impulse duration
dt=T/(N);
Nyq=N/(2*T); % nyquist frequency
df=1/T;
t = 0:2*pi/N:2*pi - 2*pi/N;
r = 0.5*square(N_p*t, 50) + 0.5; % creating a square wave with 50 duty cycle
figure
subplot(211)
bar(t/pi, r, 'green')
ylim([-0.1, 1.1]);
grid on
title('Señal')
R = fft(r)/N;
R1=R(1:N/2+1); % positive frequencies
R2=R(N/2+1:N); % negative frequencies
R=[R2,R1]; % full
nu=(-Nyq+df*(0:N))*2*pi/(N_p); % freq. vectror with normalisation
subplot(212)
bar(nu(1:N),abs(R(1:N)), 'blue')
grid on
title('Espectro');
ylim([-0.01, 0.6]);
%%
%%%%%%%%%%%%%%%%%%%%%%%--6--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
H_1 = [1, 1; 1, -1];
H = H_1;
for i = (1:5)
H = kron(H_1, H);
end
figure
subplot(211)
bar(t/pi, r, 'green')
ylim([-0.1, 1.1]);
grid on
R_H = fwht(r); % try fast Walsh-Hadamard transform
subplot(212)
%fw = 1:N;
bar(R_H, 'blue')
ylim([-0.01, 0.51]);
grid on
%%
%%%%%%%%%%%%%%%%%%%%%%%--7--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%%%%%%%%%%%%%%%%%%%%%%--8--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc