Trouble doing fourier transform of ASK using MATLAB - matlab

We have a ASK modulated signal. We need the Fourier Transform, therefore I used FFT, but do I need to abs() for only one side? And is my code complete? I'm also not sure how to transfer from time domain (t) to frequency domain (f) in order to do the matlab.
clear all;
clc;
close all;
F1=input('Enter the frequency of carrier=');
F2=input('Enter the frequency of pulse=');
A=3;
t=0:0.001:1;
x=A.*sin(2*pi*F1*t);
u=A/2.*square(2*pi*F2*t)+(A/2);
v=x.*u;
figure
subplot(4,1,1);
plot(t,x);
xlabel('Time');
ylabel('Amplitude');
title('Carrier');
grid on;
subplot(4,1,2);
plot(t,u);
xlabel('Time');
ylabel('Amplitude');
title('Square Pulses');
grid on;
subplot(4,1,3);
plot(t,v);
xlabel('Time');
ylabel('Amplitude');
title('ASK Signal');
grid on;
fs=100;
q=fft(v);
subplot(4,1,4);
plot(fs,q);
grid on;

If you are not sure if you should use abs() or not, use it. For more info.
fs=100;
q=abs(fft(v));
subplot(4,1,4);
plot(q);

Depends on how you want to look at the resulting FFT.
The function fft(v) is usually complex.
There are two ways to examine a FFT.
One the most used, is phase/magnitude plot and the other is I and Q representation, which are the coefficients of the sine and cosine harmonics.
If you want to look at the magnitude and phase, then you ask this.
abs(fft(v)) for magnitude, and angle(fft(v)) for phase
If you instead want to look at in form the I and Q channels, then ask this:
real(fft(v)) and imag(fft(v))
Shift is a good idea to move the spectrum to zero center. It makes a spectrum more comprehensible.

Related

Why i am getting same output and plot for spline and cubic?

Input of digital systems is usually analog but after processing signals in digital form they give back output in analog form
I am trying to write a code for reconstruction of signal in matlab using interpolation technique but i am getting same output/plot for both interpolation types as shown in last plot of code.
My code is below:
t= 0:0.001:1;
fm= 10
fs= 8*48
x= sin(2*pi*fm*t) % Message Signal
% Plotting discrete time sampled signal x[n]
% Pulse Traain
d= 0:1/50:1;
y= pulstran(t,d,'rectpuls',0.001)
% Sampling
z= x.*y
% Non-uniformly quantize the discrete time signal using u-law companding
% method, u= 100 and number of bits= 8.
% Quantization
N= 8
V= max(x)
u= 100
compsig= compand(x,u,V,'mu/compressor');
L= 2.^N
D= [max(compsig)-min(compsig)]./(L-1);
quants= quant(compsig,D);
xq= compand(quants,u,max(quants),'mu/expander')
% Encode the Signal into discrete levels.
H_e= dsp.UniformEncoder(max(xq),N);
encoder= step(H_e,xq)
% Decoding the signal from discrete level and reconstruct using spline and cubic
% interpolation to reconstruct the analog signal x'(t) from the
% discrete time signal using $t= 0.001.
H_d= dsp.UniformDecoder(max(xq),N);
decoder= step(H_d,encoder)
% Cubic Interpolation
time= 0:0.0001:1;
ci= interp1(t,decoder,time,'cubic')
% Spline interpolation
time=0:0.0001:1
si= interp1(t,decoder,time,'spline')
figure(01)
subplot(2,1,1)
plot(t,x,'R','LineWidth',2)
xlabel('Time')
ylabel('Amplitude')
legend('x')
title('Message Signal')
subplot(2,1,2)
plot(t,y,'B','LineWidth',2)
xlabel('Time')
ylabel('Amplitude')
legend('y')
title('Pulse Train')
figure(02)
subplot(2,1,1)
plot(t,z,'C','LineWidth',2)
xlabel('Time')
ylabel('Amplitude')
legend('z')
title('Sampled Signal')
subplot(2,1,2)
plot(t,xq,'G','LineWidth',2)
xlabel('Time')
ylabel('Amplitude')
legend('xq')
title('Quantized Signal')
figure(03)
subplot(2,1,1)
plot(t,encoder,'M','LineWidth',2)
xlabel('Time')
ylabel('Amplitude')
legend('encoder')
title('Encoded Signal')
subplot(2,1,2)
plot(t,decoder,'K','LineWidth',2)
ylim([-1 1])
xlabel('Time')
ylabel('Amplitude')
legend('decoder')
title('Decoded Signal')
figure(04)
subplot(2,1,1)
plot(time,ci,'B','LineWidth',2)
ylim([-1 1])
xlabel('Time')
ylabel('Amplitude')
legend('ci')
title('Cubic Interpolation')
subplot(2,1,2)
plot(time,ci,'R',time,si,'G')
xlabel('Time')
ylabel('Amplitude')
legend('si')
title('Spline & cubic Interpolation')
How can i see difference in output/plots of both types of interpolation?
There's only one correct reconstructed result, and both interpolation methods are trying to approximate that.
Your input is such that they both do a good job, so you just can't see any difference.
You will need some signal content near Fs/2 in order to see any difference, and even then it might be very difficult by eye to see which is better.
The reason both interpolators are working will is that they function like low-pass filters with cut-off frequency around Fs/2. They both have good response far below that frequency and not-so-good response near that frequency.
You can do a better job of reconstructing signals with content near Fs/2 by first interpolating with a good digital low-pass filter to multiply the sampling frequency by 8 or so, and then you can use cubic interpolation to get any values for any points between those new samples. You can even adjust your digital low-pass to correct for the small errors that cubic interpolation will eventually cause, to produce a very accurate result.

FFT of Gaussian Pulse

I'm trying to get Fourier transform of a gaussian pulse. Here's a sample code I found on the Internet.
fs=80; %sampling frequency
sigma=180;
t=-0.5:10/fs:0.5; %time base
variance=sigma^2;
x=1/(sqrt(2*pi*variance))*(exp(-t.^2/(2*variance)));
subplot(2,1,1)
plot(t,x,'b');
title(['Gaussian Pulse \sigma=', num2str(sigma),'s']);
xlabel('Time(s)');
ylabel('Amplitude');
L=length(x);
NFFT = 1024;
X = fftshift(fft(x,NFFT));
Pxx=X.*conj(X)/(NFFT*NFFT); %computing power with proper scaling
f = fs*(-NFFT/2:NFFT/2-1)/NFFT; %Frequency Vector
subplot(2,1,2)
plot(f,abs(X)/(L),'r');
title('Magnitude of FFT');
xlabel('Frequency (Hz)')
ylabel('Magnitude |X(f)|');
xlim([-10 10])
I need an explanation. Assume that I'm trying to get Fourier transform of 100 femtosecond gaussian pulse. How can I calculate the sampling frequency, sigma and t variables.
http://www.gaussianwaves.com/2014/07/generating-basic-signals-gaussian-pulse-and-power-spectral-density-using-fft/
A gaussian pulse has infinite support so there's no such thing as a "100 femtosecond gaussian pulse", you can however have a gaussian pulse with a sigma of 100 femtoseconds which is probably what you want. For your sampling frequency, you probably want to choose something such that you actually capture interesting information about the pulse. Your sampling period hence be less than a sigma (the original code (not the one you posted) from the blog used sigma/8). As for t, if you want to capture most of the energy in the pulse, you need to go several sigmas to the left and right of 0 which is where the pulse is centered in time.

Convolution Theorem via the fft in MATLAB

I'm in the process attempting to convolve and export an audio signal y(t) with a frequency response h(t) in different ways for a MATLAB project. It's been straightforward for the most part, however, I've run into difficulty when trying to convolve the signals using the Convolution Theorem.
I'm able to take the Fourier Transforms of both signals easily using the fft() function, but when I multiply those two results together then use the ifft() functionto find my final signal, the program always outputs garbage. I've tried playing around with padding the input with zeros, but it hasn't done much.
Here's the gist of the code I have at the moment (plot functions were removed for readability).
Y = fft(y);
H = fft(h);
F = Y*H;
f = ifft(F);
For those who are interested, the audio file is a 38 second long .wav file with a sample rate of 22050. The impulse response is the Cosine function between -pi/2 and pi/2.
Thanks in advance, any help is greatly appreciated.
I am a bit rusty with the Convolution Theorem, so I can say something wrong; but I will highlight two things:
The product in frequency should be an element-wise multiplication, i.e.
F=Y.*H.
The frequency response should be a cosine in frequency, not in time. I guess you want a frequency response that does low-pass filtering.
I played a bit with these ideas, and this is what I got:
clear all; close all; clc;
% Signal
load handel.mat;
%sound(y,Fs)
N = numel(y);
t = linspace(0,(N-1)/Fs,N);
% Response
H = abs(cos(linspace(0,pi,N))).';
% FFT product
Y = fft(y);
h = abs(ifft(H));
F = Y.*H;
f = abs(ifft(F));
% Plot
figure('Name','Time-domain');
subplot(3,1,1); plot(t,y); title('Signal');
subplot(3,1,2); plot(t,h); title('System');
subplot(3,1,3); plot(t,f); title('Output');
figure('Name','Frequency-domain');
subplot(3,1,1); plot(abs(Y)); title('Signal');
subplot(3,1,2); plot(abs(H)); title('System');
subplot(3,1,3); plot(abs(F)); title('Output');
% Play
sound(y,Fs);
sound(f,Fs);
In the frequency domain, it looks like the low-pass filtering is working. However, if you listen to the audio, the result for the Handel track is not amazing.
Don't forget to have a look at the good answer of Luis Mendo in here.

STFT computation without using spectrogram function!

I was trying to plot STFT using plot3 in MATLAB but failed. Can somebody guide me how to do that? My MWE is given below:
%% STFT Computataion
clear; clc; clf;
%% Get input and calculate frame size and overlap/shift
[Y,Fs]=wavread('D_NEHU_F0001_MN_10001');
frame_size=round(20*Fs/1000); % calculate frame size for 20ms
frame_shift=round(10*Fs/1000); % calculate frame shift for 10ms
%% Plot the input signal in time domain
t=1/Fs:1/Fs:(length(Y)/Fs);
subplot(2,1,1)
plot(t,Y);
title('Speech signal in time domain');
ylabel('Magnitude of samples');
xlabel('time in seconds');
%% Calculation of STFT
%NoOfFrames=floor((length(Y)/frame_shift)-1);
NoOfFrames=length(Y)-frame_size;
j=1;
%for i=1:frame_shift:(length(Y)-frame_size)
for i=1:frame_shift:((length(Y)-frame_size))%+frame_shift)
sp_frame=Y(i:(i+frame_size)).*hamming(frame_size+1);
sp_frame_dft=abs(fft(sp_frame)); % Compute STFT
sp_frame_array(:,j)=sp_frame_dft;
j=j+1;
end
%% Plot the STFT in 3D
[rows,cols]=size(sp_frame_array);
F=linspace(1/Fs,Fs/2000,cols);
T=1/Fs:(frame_shift*Fs/1000):(cols*(frame_shift*Fs/1000));
Z=1:frame_size+1;
subplot(2,1,2)
%mesh(sp_frame_array);
%surf(sp_frame_array,'EdgeColor','none');
plot3(T,F,sp_frame_array);
I am not sure what your question exactly is about, but I guess the problem is, with the provided code, that you do not get a plot similar to the one you'd get, say, with surf.
Furthermore, I am also not quite sure why you would want to use plot3, maybe to get the labels on the time and frequency right ? you could do that all the same with surf:
surf(T, F, sp_frame_array,'EdgeColor','none');
As a matter of fact, the reason why your plot3 does not give the same figure is because the arguments of plot3 must be three matrices of the same size (check it on help plot3). Your code should actually be broken on Matlab, which it's not, according to my test. Well, once again Matlab allowing people to mess around without warnings (go Python! :D)... Anyway, try to set the matrices more like the following:
F=linspace(1/Fs,Fs/2000, rows); % note: has to be rows, not cols here!
Fmat = F(:) * ones(1,cols); % or use repmat
T=1/Fs:(frame_shift*Fs/1000):(cols*(frame_shift*Fs/1000));
Tmat = ones(rows,1) * T(:)';
plot3(Tmat,Fmat,sp_frame_array);
While this will normally produce something more in line with what I would expect in drawing a spectrogram, I'd still make some remarks:
your F vector should go up to Fs, because of the way you filled sp_frame_dft in. More specifically, it should go from 0Hz to Fs - Fs/rows:
F = linspace(0,Fs*(1-1/rows)/1000,rows); % in kHz
you would probably like to draw the amplitudes in dBs:
plot3(Tmat,Fmat,db(sp_frame_array));
plot3 draws one line per column of the provided matrices. That means potentially lots of lines to draw! As #atul-ingle asked, are you sure this is what you want? Maybe waterfall would provide a better rendering at a lower cost?
waterfall(T,F,db(sp_frame_array));
Well, you'll get the lines for the rows, instead of the columns, so you might need to transpose if the latter is what you want.
You might also prefer to visualise only the first half of the matrix (because the frequencies higher than Fs/2 are only mirrors of the other half of the matrix).
Hope that helps!

PSD estimation via FFT

I estimate Power Spectral Density (PSD) for Gaussian monopulse in UWB band. I have two codes, using fft. But there is a problem with y axes, since I don't know its dimension (I need PSD in dBm/MHz). And also it should be a mistake in the first code, since it shows only one value at the y axes.
Code1
fs=1e11;
g=0.1e-9;
tmax=1e-9;
fftl=2048;
t=(-tmax:1./fs:tmax)';
s=t./(g.^3.*sqrt(2.*pi)).*exp(-t.^2./(2.*g.^2))./2.5e19;
figure(1)
plot(t,s);
xlabel('Time, s');
ylabel('Amplitude, V');
ffts=abs(fft(s,fftl));
ffts=2.*ffts./fftl;
fftp=abs(ffts.*conj(ffts))./2;
fftps=(fftp-30)./1e-6;
f=0:fs./fftl:fs./2-fs./fftl;
figure(2)
plot(f,fftps(1:length(f))),grid;
xlabel('Frequency, Hz');
Code2
fs=1e11;
g=0.1e-9;
tmax=1e-9;
t=(-tmax:1./fs:tmax)';
s=t./(g.^3.*sqrt(2.*pi)).*exp(-t.^2./(2.*g.^2))./2.5e19;
figure(1)
plot(t,s);
xlabel('Time, s');
ylabel('Amplitude, V');
S=fft(s,8192);
f=fs.*(0:4095)./8192;
Pss=S.*conj(S)./8192;
figure(2)
plot(f,Pss(1:4096));
Thank you so much for any help!
Your second plot should give you a plot with units V^2/Hz.
dBm units are measure of power relative to 1 mW, so you have to know the impedance of your measurement.
So to get to mW/Hz you would want to multiply by 1e6/R, where R is your impedance. Then take 10*log10 of the result and you have dBm/Hz.