MATLAB - Using fm demod to decode data - matlab

I am trying to extract a sinusoid which itself has a speed which changes sinusiodially. The form of this is approximately sin (a(sin(b*t))), a+b are constant.
This is what I'm currently trying, however it doesnt give me a nice sin graph as I hope for.
Fs = 100; % Sampling rate of signal
Fc = 2*pi; % Carrier frequency
t = [0:(20*(Fs-1))]'/Fs; % Sampling times
s1 = sin(11*sin(t)); % Channel 1, this generates the signal
x = [s1];
dev = 50; % Frequency deviation in modulated signal
z = fmdemod(x,Fc,Fs,fm); % Demodulate both channels.
plot(z);
Thank you for your help.

There is a bug in your code, instead of:
z = fmdemod(x,Fc,Fs,fm);
You should have:
z = fmdemod(x,Fc,Fs,dev);
Also to see a nice sine graph you need to plot s1.
It looks like you are not creating a FM signal that is modulated correctly, so you can not demodulate it correctly as well using fmdemod. Here is an example that does it correctly:
Fs = 8000; % Sampling rate of signal
Fc = 3000; % Carrier frequency
t = [0:Fs]'/Fs; % Sampling times
s1 = sin(2*pi*300*t)+2*sin(2*pi*600*t); % Channel 1
s2 = sin(2*pi*150*t)+2*sin(2*pi*900*t); % Channel 2
x = [s1,s2]; % Two-channel signal
dev = 50; % Frequency deviation in modulated signal
y = fmmod(x,Fc,Fs,dev); % Modulate both channels.
z = fmdemod(y,Fc,Fs,dev); % Demodulate both channels.
If you find thr answers useful you can both up-vote them and accept them, thanks.

Related

Optical frequency comb time to frequency domain - MATLAB

I am trying to generate ultrashort pulses and then seeing the resulting frequency comb using a fourier transform, I have used the gaussian pulse and pulse train functions to try and do this but it is not coming out correctly - I am hoping to be able to change the variables at the top to see the changes quickly
If here is a solution or any good resources that could help me I would appreciate it alot... Thanks
Code is here:
fs = 1e17 ; % sample rate
frep = 7.5e9; % repition rate
f_sig = 1.93e15; %frequency of signal
tc = gauspuls('cutoff',f_sig,100,[],-80);
t = -tc*200:1/fs:tc*200;
[x1,x2,x3] = gauspuls(t,f_sig,0.5);
figure(1);
plot(t,x1,t,x3)
xlabel('Time (s)')
ylabel('Waveform')
ts = 0:1/fs/2:tc*50000000 ;
d = 0:1/frep:tc*50000000 ; %delay
y = pulstran(ts,d,x,fs);
figure(2);
plot(ts,y)
%Frequency Comb FFT
fsamp = fs;
L= length(t); %signal length
NFFT = 2^nextpow2(L);
FFT = abs(fftshift(fft(x3,NFFT))/NFFT); %FFT with FFTshift for both negative & positive frequencies
f = fsamp*(-NFFT/2:(NFFT/2-1))/NFFT; %Frequency Vector
figure(3)
plot(f/1e9,10*log10(FFT/1e-3));
title('Magnitude of FFT');
xlabel('Frequency (GHz)');
ylabel('Magnitude |X(f)|');
%xlim([-100 100])

DFT code on Matlab does not work as intended

I am trying to implement a basic DFT algorithm on Matlab.
I simply use in phase and quadrature components of a sine wave with phase modulation(increasing frequency a.k.a chirp). I do compare my results with fft command of Matlab. My code gives the same results whenever there is no phase modulation(pure sine). Whenever I add chirp modulation, results differ. For example, when I use a chirp with some bandwidth around a carrier, the expected results should be a frequency distribution of chirp bandwidth starting from carrier frequency. However, I get a copy of that result backwards starting from carrier frequency as well. You can use my code below without modifying anything. Figure 5 is my result and figure 6 is the expected result. Carrier is 256 Hz with a 10Hz bandwidth of chirp. You can see the code below. The important part is for loop where I take dft of my signal. Also uou can see my dft result below.
close all;
clear all;
%% signal generation
t = (0:0.0001:1); % 1 second window
f = 256; %freq of input signal in hertz
bw = 10; % bandwidth sweep of signal
phaseInput = 2*pi*t*bw.*t;
signalInput = sin(2*pi*f*t + phaseInput); %input signal
inphase = sin(2*pi*f*t).*cos(phaseInput); %inphase component
quadrature = cos(2*pi*f*t).*sin(phaseInput); %quadrature component
figure
plot(t,signalInput,'b',t,inphase,'g',t,quadrature,'r');
title('Input Signal');
xlabel('Time in seconds');
ylabel('Amplitude');
%% sampling signal previously generated
Fs = 1024; %sampling freq
Ts = (0:1/Fs:1);%sample times for 1 second window
sPhase = 2*pi*Ts*bw.*Ts;
sI = sin(2*pi*f*Ts).*cos(sPhase);
sQ = cos(2*pi*f*Ts).*sin(sPhase);
hold on;
plot(Ts,sI+sQ,'b*',Ts,sI,'g*',Ts,sQ,'r*');
fftSize = Fs; %Using all samples in dft
sampleIdx = (0:1:fftSize-1)';
sampledI = sI(1:fftSize)';
sampledQ = sQ(1:fftSize)';
figure;
plot(sampleIdx,sampledI,sampleIdx,sampledQ);
title('Sampled IQ Components');
%% DFT Calculation
dftI = zeros(fftSize,1);
dftQ = zeros(fftSize,1);
for w = 0:fftSize-1
%exp(-2*pi*w*t) = cos(2*pi*w*t) - i*sin(2*pi*w*t)
cI = cos(2*pi*w*sampleIdx/fftSize); %correlation cos
cQ = -sin(2*pi*w*sampleIdx/fftSize); %correlation sin
dftI(w+1) = sum(sampledI.*cI - sampledQ.*cQ); %
dftQ(w+1) = sum(sampledI.*cQ + sampledQ.*cI);
end;
figure;
plot(Fs*sampleIdx/fftSize,dftI);
title('DFT Inphase');
xlabel('Hertz');
figure
plot(Fs*sampleIdx/fftSize,dftQ);
title('DFT Quadrature');
xlabel('Hertz');
figure;
plot(Fs*sampleIdx/fftSize,sqrt(dftQ.^2+dftI.^2));
%% For comparison
sampledInput = sin(2*pi*f*Ts + sPhase);
Y = fft(sampledInput(1:1024),1024);
Pyy = Y.*conj(Y)/1024;
f = (0:1023);
figure;
plot(f,Pyy)
title('Power spectral density')
xlabel('Frequency (Hz)')
the reason lies in the fact that two different signals will definitely give your two different frequency spectrums. check out the code below, you will find that the input of the dft algorithm you actually gave is sampledI+jsampledQ. as a result, what you are doing here is NOT merely decomposing your original signal into In-phase and quadrature components, instead, you are doing Hilbert transform here -- to change a real signal into a complex one.
cI = cos(2*pi*w*sampleIdx/fftSize); %correlation cos
cQ = -sin(2*pi*w*sampleIdx/fftSize); %correlation sin
dftI(w+1) = sum(sampledI.*cI - sampledQ.*cQ); %
dftQ(w+1) = sum(sampledI.*cQ + sampledQ.*cI);
so the sampledInput for comparison should be sampledInput = sI+1i*sQ;.

Frequency Modulation Matlab

i want to make a continious sinwave which will change his frequency 3 times, like in the photo. I tryied with the fmmod function but i can't make it work, so is there any other way , thanks in advance.
here is my code
Fs = 44100; % Sampling rate of signal
Fc = 3000; % Carrier frequency
t = [0:Fs-1]'/Fs; % Sampling times
x = sin(2*pi*4000*t)+sin(2*pi*5000*t)+sin(2*pi*8000*t); % my signal
dev = 50; % Frequency deviation in modulated signal
y = fmmod(x,Fc,Fs,dev);
plot(y)
Frequency modulation without fmmod:
Fs = 44100; % Sampling rate of signal
T=1/Fs; % period
t = 0:T:0.01; % sample times
% amplitude of message signal
vm = 5
% amplitude of carrier signal
vc =5
% message frequency
fm =500
% carrier freq
fc = 5e3
% modulation index
m =5
figure
y = vc*sin(2*pi*fc*t+m.*cos(2*pi*fm*t));
plot(t,y);

Find value of frequency form Sin wav

i have a this following equation of sine wav
Fs = 8000; % Sampling rate of signal
Fc = 3000; % Carrier frequency
t = [0:Fs-1]'/Fs; % Sampling times
dev = 50; % Frequency deviation in modulated signal
s1 = sin(2*pi*200*t)+2*sin(2*pi*f*t);
now i want to value of f for s1 equation how can we get this? thanks
You may want to look around SO for similar questions.
The FFT is the simplest route to a solution:
spec = abs(fft(s1));
Then search for the maxima in the spectrum using a detection threshold.
Example (here f=10):
f= 10;
s1 = sin(2*pi*200*t)+2*sin(2*pi*f*t);
thresh = 0.2;
f1=abs(fft(s1))/sum(abs(s1));
f= [0:length(f1)-1]/length(f1)*Fs;
f(f1(1:end/2)>0.2)
This is the result (the frequencies in the spectrum for peaks with amplitude greater than the threshold value):
ans =
10 200

Modulation and demodulation in MATLAB

I modulated two channels using fmmod() method and demodulate that modulated signal using the fmdemod() method. How can we get back those two channels that are modulated? How can we do this?
My code is:
Fs = 8000; % Sampling rate of signal
Fc = 3000; % Carrier frequency
t = [0:Fs-1]'/Fs; % Sampling times
s1 = sin(2*pi*300*t)+2*sin(2*pi*600*t); % Channel 1
s2 = sin(2*pi*150*t)+2*sin(2*pi*900*t); % Channel 2
x = [s1,s2]; % Two-channel signal
dev = 50; % Frequency deviation in modulated signal
y = fmmod(x,Fc,Fs,dev); % Modulate both channels.
z = fmdemod(y,Fc,Fs,dev); % Demodulate both channels.
figure(1);
plot(z);
What type of value does variable z contain? How do I get back channels from the fmdemod method?
Not sure to correctly understand your question, but you can get the channel 1 by doing:
x(1:end,1) % or x(1:end,2) for channel 2
For the value z, you can get the first demodulated channel by doing:
z(1:end,1) % or z(1:end,2) for the second demodulated channel
Is it what you need?