Frequency Modulation Matlab - 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);

Related

Matlab Acoustic Modem with QAM modulation

I am trying to make an acoustic modem with matlab. It records sound an stores it in an array, modulates it using 64-QAM and adds then demodulates it. But the demodulated sound and recorded sound are not the same.
Here is the code:
clear;
clc;
M = 64; % Modulation order (alphabet size or number of points in signal constellation)
k = log2(M);
Fs=44100;
Nbits=8;
nChannels=1;
SNR=250;
%audio recording
recObj=audiorecorder(Fs,Nbits,nChannels);
recDuration=3;
disp("Begin speaking.")s
recordblocking(recObj,recDuration);
disp("2 sec pause")
pause(2)
disp("End of recording")
y=getaudiodata(recObj);
plot(y);
% playObj=audioplayer(y,Fs);
% play(playObj);
Ts=-(1/Fs)/2:1:(1/Fs)/2;
figure
subplot(1,2,1);
plottf(y,Ts,'t');
subplot(1,2,2);
plottf(y,Ts,'f');
%MODULATION QAM
y_int = abs(round(y*1000/16));
constdiag = comm.ConstellationDiagram('XLimits',[-sqrt(M) sqrt(M)],'YLimits',[-sqrt(M) sqrt(M)]);
qamData = qammod(y_int,M);
constdiag(qamData)
%DEMODULATION QAM
demodData = qamdemod(noisySound,M);
demodData = demodData*16/1000;
isequal(y_int,demodData);
playObjn=audioplayer(demodData,Fs);
play(playObjn);
%LOWPASS FILTER
Fn = Fs/2; % Nyquist Frequency (Hz)
Wp = 1000/Fn; % Passband Frequency (Normalised)
Ws = 1010/Fn; % Stopband Frequency (Normalised)
Rp = 1; % Passband Ripple (dB)
Rs = 150; % Stopband Ripple (dB)
[n,Ws] = cheb2ord(Wp,Ws,Rp,Rs); % Filter Order
[z,p,k] = cheby2(n,Rs,Ws,'low'); % Filter Design
[soslp,glp] = zp2sos(z,p,k); % Convert To Second-Order-Section For Stability
figure(3)
freqz(soslp, 2^16, Fs) % Filter Bode Plot
filtered_sound = filtfilt(soslp, glp, demodData);
%sound(filtered_sound, 44100)
where am i doing wrong?
I tried 16QAM and 32QAM but result is the same.

Scaling amplitudes by two for the FFT in MATLAB

I just read the example of Mablab tutorial, trying to studying the FFT function.
Can anyone tell me that for the final step, why P1(2:end-1) = 2*P1(2:end-1). In my opinion, it is not necessary to multiply by 2.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
%--------
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
%---------
X = S + 2*randn(size(t));
%---------
plot(1000*t(1:50),X(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('t (milliseconds)')
ylabel('X(t)')
Y = fft(X);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
Y = fft(S);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
plot(f,P1)
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
Matlab sample
The reason for the multiplication by 2 is that the spectrum returned by fft is symmetric about the DC component. Since they are showing the single-sided amplitude spectrum, the amplitude of each point is going to be doubled to account for the contributions of data on the other side of the spectrum. For example, the single-sided amplitude of pi/4 is the amplitude at pi/4 plus the amplitude at -pi/4.
The first sample is skipped since it is the DC point and therefore shared between the two sides of the spectrum.
So for example, if we look at the fft of their example signal with a 50Hz sinusoid of amplitude 0.7 and a 120Hz sinusoid of amplitude 1.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
% Compute the FFT
Y = fft(S);
% Compute the amplitudes
amplitude = abs(Y / L);
% Figure out the corresponding frequencies
f = Fs/L*[0:(L/2-1),-L/2:-1]
% Plot the result
plot(f, amplitude)
When we plot this, you'll see that it's symmetric and the original input amplitude is only realized by combining the amplitudes from both sides of the spectrum.
A slightly more explicit version of what they have done would be to be the following which sums the two halves of the spectrum
P1(2:end-1) = P1(2:end-1) + P2((L/2+2):end);
But since by definition the spectrum is symmetric, the opt to simply multiply by 2.

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?

MATLAB - Using fm demod to decode data

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.

Matlab fft function

The code below is from the Matlab 2011a help about fft function.
I think there is a problem here : why do they multiply t(1:50) by Fs, and then say it's time in millisecond ? Certainly, it happens to be true in this very particular case, but change the value of Fs to, say, 2000, and it won't work anymore, obviously because of this factor of 2. Right ? Quite misleading, isn't it ? What do I miss ?
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t)); % Sinusoids plus noise
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
Clearer with this :
fs = 2000; % Sampling frequency
T = 1 / fs; % Sample time
L = 1000; % Length of signal
t2 = (0:L-1)*T; % Time vector
f = 50; % signal frequency
s2 = sin(2*pi*f*t2);
figure, plot(fs*t2(1:50),s2(1:50)); % NOT good
figure, plot(t2(1:50),s2(1:50)); % good
You don't miss anything. This is a typical bad practice of not having units as comments for each line. For example,
Fs=1000; % in [Hz]
T=1/Fs; % [sec]
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
then, the time vector has units of 1/1000 seconds, or milliseconds... (each element is 1 ms).
In the other case Fs=2000 %[Hz], which makes the time vector to have units of 1/2000 seconds...