%HELSINKI UNIVERSITY OF TECHNOLOGY;
%Communications laboratory;
%Seppo Saastamoinen 25.10.2003;

%MACRO: apua.m (apua = help in English), ver. 1.0;

%YOU CAN COPY COMMANDS AND ROWS FROM THIS MACRO AND MODIFY THEM;
%TO MAKE YOUR OWN MACRO TO RUN.;

%DON'T RUN THIS MACRO apua.m, THIS MACRO DO NOTHING!;
%IN THE END IS AN IDEAL BANDPASS FILTER AND THE PLOT;
%SECTION WHICH WORKS;
%YOU CAN COPY THOSE PARTS TO YOUR OWN MACRO IF NEEDED;

%WHEN YOU MAKE YOUR OWN MACRO DON'T DELETE COMMANDS AND ROWS.;
%INSTEAD OF ADD THE % CHARACTER BEFORE A COMMAND OR A ROW.;
%IN MOST CASES YOU WILL LATER NEED YOUR "RESERVE" COMMANDS OR ROWS!;

%PLEASE ADD THE ; CHARACTERS AFTER YOUR COMMANDS.;
%OTHERWISE YOUR COMMAND WINDOW IS FULL OF NUMBERS;
%AND THE TIME NEEDED TO RUN YOUR MACRO IS LONGER.;

%1. Setting a value to a parameter, e.g. a sample frequency;
%Fs = 100000; %Sample interval Ts = 1/Fs;

%2. Making a vector, e.g. 1 s time vector:;
%t = start:step:end; %e.g. t = 0:Ts:1-Ts;

%3. Making a vector containing ones, e.g.;
%Z = ones(length,1); %e.g. Hf = ones(max(size(t));1);

%4. Making a vector containing zeros, e.g.;
%U = zeros(length,1); %e.g. Hf1 = zeros(1024,1);

%5. Picking up values from a vector to an another vector, e.g.;
%V = F(start:stop); %e.g. V = Xf(0:200);

%6. Putting values to a vector, e.g.;
%G(start:stop) = value; %e.g. Hf1(0:1188) = 0;

%7. Example: making an ideal lowpass filter;
%Hfx = ones(44100,1); Hfx(0:12000) = 0; Hfx(34050:44100) = 0;
%so the Hfx(12001:19999) = 1 = the lowpass area;

%8. Multiplying vectors, e.g.;
%xt.*cos(2*pi*fc*t); %e.g. yourvoice.*cos(2*pi*1550*t);
%Please remark the . character after the first vector;
%pi = 3.141...;

%9. The number of a vector elements; e.g.;
%N = max(size(vector's name)); %e.g. N = max(size(xt));

%10. The maximum and minumum values of a vector, e.g.;
%maxi = max(H); mini = min(I);

%11. The mean and standard deviation values of a vector, e.g.;
%m = mean(vector's name); sd = std(vector's name);

%12. Example: Calculating DC, AC and total average power:;
%PDC = m.*m/Z; PAC =sd.*sd/Z; Pave = PDC + PAC;
%Z = impedance level of a system;

%13. Calculating the Fourier transform, FFT, Fast Fourier Transform, e.g.;
%Xf = fft(xt)/N; %Value of the numbers per vector is needed;
%to calculate right values!; %N = max(size(xt));
%Remark: Xf has complex form, e.g. Xf(100) = 0.123 + 0.456i etc.;

%14. Calculating an amplitude spectrum in the 1-sided frequency domain;
%Xfamp = 2*abs(fft(xt))/N;

%15. Making a 2-sided spectrum in the frequency domain, e.g.;
%Xf2sided = fftshift(Xf);

%16. The inverse Fourier transform, IFFT, e.g.;
%xtout = real(ifft(Xfx))*Nf; %Nf = max(size(Xfx));
%The command real is needed because of all of our life signals are;
%real signals.;
%Xfx must be in the complex form!;

%17. Plotting, e.g.;
%plot (x,y); %e.g. plot(t,xt) or plot(f,Xf);

%18. Plotting many curves to the same figure;
%plot (x1, y1, x1, y2, ...);
%Remark: x-scale is the same, y-scales can be diffrent.;

%19. Many figures to an one page? Use subplot;
%subplot(Y,X,number); %e.g. subplot(3,2,1) means that you;
%have 2 figures in the horizontal zone [vaakasuunta], 3 figures in;
%the vertical zone [pystysuunta] and last ,1 means;
%that the 1st figure is plotted;
%The plot command comes after a subplot command, e.g.;
%subplot(2,2,3); %plot (xt,t);

%20. Too long macro listing? You can put several;
%commands to the same line, e.g.
%Fs = 100000; Ts = 1/Fs; t = 0:Ts:1-Ts; xt = cos(2*pi*1000*t);

%21. Setting the axis values in plotting, e.g.;
%axis ([0 max(size(t))*Ts 1.2*min(qam1) 1.2*max(qam1)]);
%Remark the automation concerning the scales.;

%21. Giving names to the x- and y-scales, e.g.;
%xlabel ('Time/s'); ylabel ('Output of the QAM modulator');

%22. Setting grid and zoom on, e.g.;
%grid on; zoom on;

%23. Too many figures on the same page, the figures are too small?;
%You can plot many pages by using the command figure before;
%1st plot- or subplot command, e.g.;
%figure;

%24. Setting the title for a page, e.g.;
%title({'Typeheretextforthe1strow','Typeheretextforthe2ndrow'});

%25. An example for plotting:;
%figure; %subplot(2,2,1); %plot(t,xt);
%axis ([0 max(size(t))*Ts 1.2*min(xt) 1.2*max(xt)]);
%%xlabel ('Time/s'); ylabel ('Input signal to the SSB modulator');
%grid on; zoom on;
%title({'A LSSB modulator - input signals','Jussi and Pekka, 13.11.2003'});

%26. Convolution, e.g.;
%yt = conv(xt,ht)/Fs; %Remark the division by Fs!;

%27. The size of frequency step when plotting spectrums, e.g.;
%fs = 1/(max(t)-min(t)); %fs = Fs/N; %fs = 1/(N*Ts);

%28. Setting the frequency vector, e.g.;
%fre = 0:fs:Fs-fs; %Remark the 1st cell is 0, so the last is Fs-fs;

%29. Loading vector from a file, e.g.;
%load d:\data\sat3; xt4 = sat3; Remark the command pair!;

%30. Loading a voice signal in wav form from a file, e.g.;
%[sound1,Fs] = wavread('d:\data\matlab\myvoice1.wav');
%Remark that you get the sample frequency at the same time.;

%31. Finding values from a vector, e.g.
%MAKS = find(max(xt)); %Value = find(xt>70);
%So you get the pointers for a vector to find places of the;
%interesting values.;

%Integrating, e.g.;
%xti = cumsum(xt)*Ts; %Integral from xt(0) to xt(end);
%xti is a vector (integral result);

%Diffrentiation, e.g.;
%xtd = diff(xti)/Ts;
%Please note that the length of xtd is 1 cell shorter than xti has!
%For plotting you have to make new time or frequency vector.

%32. Example: Making an ideal bandpass filtering, e.g.;
%Xf = fftshift(((fft(xt))/N); %Making the 2-sided (complex) spectrum;
%0 Hz is on the center of the vector and all the lements of the;
%vector Xf are in the complex form as 1+2i etc.;
%Hf = 0.5*zeros(max(size(Xf)),1); %Making the zeros vector;
%0.5 is needed for the 2-sided spectrum.
%...Calculating "in your head"...
%...The center element = 0 Hz, ...the value of fs (frequency step);
%...Calculations which elements must be ones... and then;
%Hf(a:b) = 1 %AND %Hf(d:c) = 1;
%So you have the vector like [0 0 .. 1 1 ..0 0 0 0 .. 1 1 .. 00];
%And then;
%Yf = 2*Xf.*Hf; %Now Xf is filtered by using a ideal bandbass filter!;
%2 is needed to scale Yf;

%AN EXAMPLE TO USE AN IDEAL BANDPASS FILTER;

pause; %Command pause stop the running. It's useful command if your macro;
%doesn't work correctly.

clear; %Use this command at the front of your macro if you makes several runs!
Fs = 10000; Ts = 1/Fs; t = 0:Ts:1-Ts; N = max(size(t));
xt = sin(2*pi*100*t); Xf = fftshift((fft(xt))/N); fs = 1/max(t);
Hf = 0.5*ones(max(size(Xf)),1); Hf(0:4500) = 0; Hf(4950:5050) = 0;
Hf(5500:10000) = 0; Hf = Hf'; Yf = 2*Xf.*Hf; Yfabs = abs(Yf);
f = -Fs/2:fs:Fs/2; Yf2 = fftshift(Yf);
yt = real(ifft(Yf2)*N);

subplot(3,2,1); plot(t,xt);
axis ([0 1 1.2*min(xt) 1.2*max(xt)]);
xlabel ('Time/s'); ylabel ('Input signal to the ideal bandpass filter');
grid on; zoom on;

title({'An ideal bandpass filter','Test signal is 100 Hz sine wave'...
   ,'Nakki Makkara 64447V and Joulu Pukki 67812W','22.11.2003 at 2 p.m.'});

%Hint: Putting three points as ... to the end of a row means that the;
%row continues to the next row. 

subplot(3,2,2); plot(f,abs(Xf));
axis ([-Fs/2 Fs/2 1.2*min(abs(Xf)) 1.2*max(abs(Xf))]);
xlabel ('Frequency/Hz'); ylabel ('Input signal in the frequency domain');
grid on; zoom on;

subplot(3,2,3); plot(f,Hf);
axis ([-Fs/2 Fs/2 1.2*min(Hf) 1.2*max(Hf)]);
xlabel ('Frequency/Hz'); ylabel ('The transfer function of the ideal bandpass filter');
grid on; zoom on;

subplot(3,2,4); plot(f,Yfabs);
axis ([-Fs/2 Fs/2 1.2*min(Yfabs) 1.2*max(Yfabs)]);
xlabel ('Frequency/Hz'); ylabel ('The output spectrum of the ideal bandpass filter');
grid on; zoom on;

subplot(3,2,5); plot(t,yt);
axis ([0 1 1.2*min(yt) 1.2*max(yt)]);
xlabel ('Time/s'); ylabel ('The output signal of the ideal bandpass filter');
grid on; zoom on;

subplot(3,2,6); plot(t,yt-xt);
axis ([0 1 1.2*min(yt-xt) 1.2*max(yt-xt)]);
xlabel ('Time/s'); ylabel ('The error signal yt-xt');
grid on; zoom on;

%THE PLOTTING SECTION FOR A COMMERCIAL FM SYSTEM:;

figure;
subplot(3,2,1);
plot(t,Monot)
axis ([min(t) max(t) 1.2*min(Monot) 1.2*max(Monot)]);
xlabel ('Time/s');
ylabel ('Mono (R+L) signal');
grid on;
zoom on;

title({'A tone FM system, part 1',...
'Typehereyournamesandstudentsnumbers','Typeheredateandtime'});

subplot(3,2,2);
fs1 =Fs/max(size(t));
f =0:fs1:Fs-fs1;
plot(f,Monof)
axis([0 Fs/2 -0.05 1.2*max(Monof)]);
xlabel ('Frequency/Hz');
ylabel ('Mono (R+L) signal');
grid on;
zoom on;

subplot(3,2,3);
plot(t,Stereot)
axis ([min(t) max(t) 1.2*min(Stereot) 1.2*max(Stereot)]);
xlabel ('Time/s');
ylabel ('L-R signal');
grid on;
zoom on;

subplot(3,2,4);
plot(f,Stereof)
axis([0 Fs/2 -0.05 1.2*max(Stereof)]);
xlabel ('Frequency/Hz');
ylabel ('Stereo (L-R) signal');
grid on;
zoom on;

subplot(3,2,5);
plot(t,mFMt)
axis ([min(t) max(t) 1.2*min(mFMt) 1.2*max(mFMt)]);
xlabel ('Time/s');
ylabel ('Modulating signal');
grid on;
zoom on;

subplot(3,2,6);
plot(f,mFMf);
axis([0 Fs/2 -0.05 1.2*max(mFMf)]);
xlabel ('Frequency/Hz');
ylabel ('Modulating signal');
grid on;
zoom on;

figure;

subplot(3,2,1);
plot(t,mFMt);
axis ([min(t) max(t) 1.2*min(mFMt) 1.2*max(mFMt)]);
xlabel ('Time/s');
ylabel ('Modulating signal');
grid on;
zoom on;

title({'A tone FM system, part 2',...
'Typehereyournamesandstudentsnumbers','Typeheredateandtime'});

subplot(3,2,2);
mFMfdB = 20*log10(mFMf);
plot(f,mFMfdB);
axis([0 Fs/2 -100 20]);
xlabel ('Frequency/Hz');
ylabel ('Modulating signal/dBV');
grid on;
zoom on;

subplot(3,2,3);
plot(t,fiit);
axis ([min(t) max(t) 1.2*min(fiit) 1.2*max(fiit)]);
xlabel ('Time/s');
ylabel ('Phase signal');
grid on;
zoom on;

subplot(3,2,4);
fiifdB =20*log10(fiif);
plot(f,fiifdB);
axis([0 Fs/2 -100 20]);
xlabel ('Frequency/Hz');
ylabel ('Phase signal/dBV');
grid on;
zoom on;

subplot(3,2,5);
plot(t,sFMt)
axis ([min(t) max(t) 1.2*min(sFMt) 1.2*max(sFMt)]);
xlabel ('Time/s');
ylabel ('FM in the time domain');
grid on;
zoom on;

subplot(3,2,6);
sFMfdB =20*log10(sFMf/max(sFMf));
%Find out that the max(sFMf) = 0 dB!;
plot(f,sFMfdB)
axis([0 Fs/2 -100 6+max(sFMfdB)]);
xlabel ('Frequency/Hz');
ylabel ('FM in the frequency domain/dB');
grid on;
zoom on;


