Cepstrum In MATLAB - matlab

I have tried and there is not proper function in MATLAB to calculate cepstrum.
By using commercial software and processing file 21.wav it is possible to get this result:
How can I achieve the same results with MATLAB code? When I use the cceps or rceps functions in MATLAB, the result is not even close:
%% Complex cepstral analysis
% Load Signal
A = importdata ('21.txt');
M = A(:,2);
Fs = 1552;
t = (0:length(M)-1)/Fs;
c = cceps(M);
plot(t,c)
xlabel('Time (s)')
title('Complex cepstrum')
ylim([0 0.2]);
xlim([0 4.6]);
The result is:
%% Real cepstrum
% Load Signal
A = importdata ('21.txt');
M = A(:,2);
Fs = 1552;
t = (0:length(M)-1)/Fs;
c = rceps(M);
plot(t,c)
xlabel('Time (s)')
title('Real cepstrum')
ylim([0 0.2]);
xlim([0 4.6]);
The result is:

add x and y limit to your code and then compare the result
ylim([0 0.18]);
xlim([0 4.6]);

Related

How do I compare two audio signals with different sampling frequencies in Matlab?

I'm trying to better understand upsampling and downsampling, and am using an audio signal to do so. I start with a base sampling rate of 66.15kHz, upsample by 2, run it through an LPF (sample rate of 66.15kHz, cutoff frequency of half that), then downsample by 2.
To visualize the differences between the initial and final signal, I created two spectrograms, with different sampling frequencies. There's lots of clear similarities, as well as a tighter band around the signals in the lower frequency version (as a result of the upsampling?), but I think the best thing to do is just take some sort of subtraction of the images. The problem is that the matrices are different sizes, so I can't use approaches found here or here.
Should I just upsample both of them to be the same size? Will the differences still be there?
The spectrograms in question:
Here's the code I used, some parts are commented out that may help to visualize things along the way:
%clc, clear all, close all;
NFFT = 1:10:4000; % used like F in the spectrogram fxn
WINDOW = 2000;
NOVERLAP = 0;
preupsample = audioread('audioclip.wav');
Fo = 66150;
orig_nyq = Fo/2;
U = 2;
D = 3;
postupsample = upsample(preupsample, U);
[preLPF, ~, W] = myfft(postupsample,U*Fo,length(postupsample));
% figure, plot(W,mag2db(abs(preLPF))), xlabel('Frequency (Hz)'), ylabel('Magnitude Response (dB)');
% title('Magnitude Response after upsampling');
filterorder = 100;
Fpass = orig_nyq;
Fs = U*Fo;
Rpass = 0.00057565; % .1dB passband ripple
Rstop = 1e-4; % -80dB stopband attenuation
LPF = firceqrip(filterorder,Fpass/(Fs/2),[Rpass Rstop],'passedge');
% fvtool(LPF,'Fs',Fs,'Color','White');
filtered_upsample = filter(LPF,1,postupsample);
[postLPF, ~, W] = myfft(filtered_upsample,U*Fo,length(filtered_upsample));
% figure, plot(W,mag2db(abs(postLPF))), xlabel('Frequency (Hz)'), ylabel('Magnitude Response (dB)');
% title('Magnitude Response after applying LPF');
postdownsample = downsample(filtered_upsample,D);
[postDS, w, W] = myfft(postdownsample,U*Fo/D,length(postdownsample));
% figure, plot(W,mag2db(abs(postDS))), xlabel('Frequency (Hz)'), ylabel('Magnitude Response (dB)');
% title('Magnitude Response after downsampling');
%[~,F1,T1,P1]=
figure, spectrogram(preupsample(:,1),WINDOW,NOVERLAP,NFFT,Fo,'yaxis');
%[~,F2,T2,P2]=
figure, spectrogram(postdownsample(:,1),WINDOW,NOVERLAP,NFFT,Fo*U/D,'yaxis');
% surf(T2,F2,10*(log10(abs(P2))-log10(abs(P1))),'edgecolor','none');
% view(90,-90);
% axis tight;
The contents of myfft.m:
function [X, w, W] = myfft(x, F, N)
X = fftshift(fft(x, N));
w = fftshift((0:N-1)/N*2*pi);
w(1:N/2) = w(1:N/2) - 2*pi;
W = F*w;
end

Why are these two plots different?

Here I am plotting a spectrogram twice, once using imagesc and once using the spectrogram automatic plotting. I don't know why I get different scaling results, probably some filtering by the automatic plotting function but I would like to know what exactly and how to transform it so that it matches.
fs = 44100;
% Frequency sweep signal
sw = logspace(log10(500),log10(5000),fs*5);
x = 0.95 * sin(cumsum((2*pi*sw)/fs));
N = 128;
win_size = N;
noverlap = N/2;
win = window(#blackman,win_size);
[s,f,t] = spectrogram(x,win,noverlap,N,fs,'yaxis');
%% IMAGESC --- FIGURE 1
figure(1)
imagesc(t,f/1000,20*log(abs(s)));
title('Spectrogram');
set(gca,'Ydir','Normal');
xlabel('Time (secs)');
ylabel('Frequency (kHz)');
hcb=colorbar;
title(hcb,'Spectral Magnitude (dB)');
%% test with automatic spectrogram plot ---FIGURE2
figure(2)
spectrogram(x,win,noverlap,N,fs,'yaxis');
end
Help is appreciated :)
Change your code "imagesc(t,f/1000,20*log(abs(s)))" as imagesc(t,f/1000,20*log10(abs(s))).
log() is natural logarithm.
When you calculate dB scale, you should use log10(), not log().

Fourier transform and LTI filter and frequency response in Matlab

I'm new to Matlab for LTI signal processing and wondering if anyone can help with something that I'm sure is meant to be basic. I've spent hours and hours researching and obtaining background information and still cannot obtain a clear path to tackle these problems. So far, from scratch, I have generated a signal required and managed to use the fft function to produce the signal's DFT:
function x = fourier_rikki(A,t,O)
Fs = 1000;
t = 0:(1/Fs):1;
A = [0.5,0,0.5];
N = (length(A) - 1)/2;
x = zeros(size(t));
f1 = 85;
O1 = 2*pi*f1;
for k = 1:length(A)
x1 = x + A(k)*exp(1i*O1*t*(k-N-1));
end
f2 = 150;
O2 = 2*pi*f2;
for k = 1:length(A);
x2 = x + A(k)*exp(1i*O2*t*(k-N-1));
end
f3 = 330;
O3 = 2*pi*f3;
for k = 1:length(A);
x3 = x + A(k)*exp(1i*O3*t*(k-N-1));
end
signal = x1 + x2 + x3;
figure(1);
subplot(3,1,1);
plot(t, signal);
title('Signal x(t) in the Time Domain');
xlabel('Time (Seconds)');
ylabel('x(t)');
X = fft(signal); %DFT of the signal
subplot(3,1,2);
plot(t, X);
title('Power Spectrum of Discrete Fourier Transform of x(t)');
xlabel('Time (Seconds)');
ylabel('Power');
f = linspace(0, 1000, length(X)); %?
subplot(3,1,3);
plot(f, abs(X)); %Only want the positive values
title('Spectral Frequency');
xlabel('Frequency (Hz)'); ylabel('Power');
end
At this stage, I'm assuming this is correct for:
"Generate a signal with frequencies 85,150,330Hz using a sampling frequency of 1000Hz - plot 1seconds worth of the signal and its Discrete Fourier Transform."
The next step is to "Find the frequency response of an LTI system that filters out the higher and lower frequencies using the Fourier Transform". I'm stuck trying to create an LTI system that does that! I have to be left with the 150Hz signal, and I'm guessing I perform the filtering on the FFT, perhaps using conv.
My course is not a programming course - we are not assessed on our programming skills and I have minimal Matlab experience - basically we have been left to our own devices to struggle through, so any help would be greatly appreciated! I am sifting through tonnes of different examples and searching Matlab functions using 'help' etc, but since each one is different and does not have a break down of the variables used, explaining why certain parameters/values are chosen etc. it is just adding to the confusion.
Among many (many) others I have looked at:
http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html
http://gribblelab.org/scicomp/09_Signals_and_sampling.html section 10.4 especially.
As well as Matlab Geeks examples and Mathworks Matlab function explanations.
I guess the worst that can happen is that nobody answers and I continue burning my eyeballs out until I manage to come up with something :) Thanks in advance.
I found this bandpass filter code as a Mathworks example, which is exactly what needs to be applied to my fft signal, but I don't understand the attenuation values Ast or the amount of ripple Ap.
n = 0:159;
x = cos(pi/8*n)+cos(pi/2*n)+sin(3*pi/4*n);
d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',1/4,3/8,5/8,6/8,60,1,60);
Hd = design(d,'equiripple');
y = filter(Hd,x);
freq = 0:(2*pi)/length(x):pi;
xdft = fft(x);
ydft = fft(y);
plot(freq,abs(xdft(1:length(x)/2+1)));
hold on;
plot(freq,abs(ydft(1:length(x)/2+1)),'r','linewidth',2);
legend('Original Signal','Bandpass Signal');
Here is something you can use as a reference. I think I got the gist of what you were trying to do. Let me know if you have any questions.
clear all
close all
Fs = 1000;
t = 0:(1/Fs):1;
N = length(t);
% 85, 150, and 330 Hz converted to radian frequency
w1 = 2*pi*85;
w2 = 2*pi*150;
w3 = 2*pi*330;
% amplitudes
a1 = 1;
a2 = 1.5;
a3 = .75;
% construct time-domain signals
x1 = a1*cos(w1*t);
x2 = a2*cos(w2*t);
x3 = a3*cos(w3*t);
% superposition of 85, 150, and 330 Hz component signals
x = x1 + x2 + x3;
figure
plot(t(1:100), x(1:100));
title('unfiltered time-domain signal, amplitude vs. time');
ylabel('amplitude');
xlabel('time (seconds)');
% compute discrete Fourier transform of time-domain signal
X = fft(x);
Xmag = 20*log10(abs(X)); % magnitude spectrum
Xphase = 180*unwrap(angle(X))./pi; % phase spectrum (degrees)
w = 2*pi*(0:N-1)./N; % normalized radian frequency
f = w./(2*pi)*Fs; % radian frequency to Hz
k = 1:N; % bin indices
% plot magnitude spectrum
figure
plot(f, Xmag)
title('frequency-domain signal, magnitude vs. frequency');
xlabel('frequency (Hz)');
ylabel('magnitude (dB)');
% frequency vector of the filter. attenuates undesired frequency components
% and keeps desired components.
H = 1e-3*ones(1, length(k));
H(97:223) = 1;
H((end-223):(end-97)) = 1;
% plot magnitude spectrum of signal and filter
figure
plot(k, Xmag)
hold on
plot(k, 20*log10(H), 'r')
title('frequency-domain signal (blue) and filter (red), magnitude vs. bin index');
xlabel('bin index');
ylabel('magnitude (dB)');
% filtering in frequency domain is just multiplication
Y = X.*H;
% plot magnitude spectrum of filtered signal
figure
plot(f, 20*log10(abs(Y)))
title('filtered frequency-domain signal, magnitude vs. frequency');
xlabel('frequency (Hz)');
ylabel('magnitude (dB)');
% use inverse discrete Fourier transform to obtain the filtered time-domain
% signal. This signal is complex due to imperfect symmetry in the
% frequency-domain, however the imaginary components are nearly zero.
y = ifft(Y);
% plot overlay of filtered signal and desired signal
figure
plot(t(1:100), x(1:100), 'r')
hold on
plot(t(1:100), x2(1:100), 'linewidth', 2)
plot(t(1:100), real(y(1:100)), 'g')
title('input signal (red), desired signal (blue), signal extracted via filtering (green)');
ylabel('amplitude');
xlabel('time (seconds)');
Here is the end result...

MATLAB - FM Modulation

I am trying to Frequency modulate a sine signal using Matlab. I have written the following code for the same:
fc = 5000; %Carrier Frequency
fs = 1000; %Signal Frequency
t = 0:0.00001:0.002;
x = sin(2*pi*fs*t);
dev = 50;
subplot(2,1,1);
plot(t,x);
y = fmmod(x,fc,fs,dev);
subplot(2,1,2);
plot(t,y);
It is able to display the first plot command but not the second one. It throws an error: `fmmod' undefined near line 10 column 5. What is wrong in the above code?
The following function will generate a FM modulated signal - it's not as good (flexible, etc) as fmmod but if you don't have the Comm System Toolbox this may be a good alternative.
function [s t] = makeFM( x, Fc, Fs, strength )
% for a signal x that modulates a carrier at frequency Fc
% produce the FM modulated signal
% works for 1 D input only
% no error checking
x = x(:);
% sampling points in time:
t = ( 0 : numel( x ) - 1 )' / Fs;
% integrate input signal
integratedX = cumsum( x ) / Fs;
s = cos( 2 * pi * ( Fc * t + strength * integratedX ));
Put this in your path and call it with similar arguments to the fmmod function (but without optional parameters):
fc = 5000; %Carrier Frequency
fs = 1000; %Signal Frequency
t = 0:0.00001:0.002;
x = sin( 2*pi*fs*t );
dev = 50;
subplot(2,1,1);
plot(t,x);
y = makeFM(x, fc, 2.5*fc, dev); % note sampling frequency must be > carrier frequency!
subplot(2,1,2);
plot(t,y);
Let me know how that works for you.
I guess this is more simple approach
clc;
clear all;
close all;
fm=input('Message Frequency=');
fc=input('Carrier Frequency=');
mi=input('Modulation Index=');
t=0:0.0001:0.1;
m=sin(2*pi*fm*t);
subplot(3,1,1);
plot(t,m);
xlabel('Time');
ylabel('Amplitude');
title('Message Signal');
grid on;
c=sin(2*pi*fc*t);
subplot(3,1,2);
plot(t,c);
xlabel('Time');
ylabel('Amplitude');
title('Carrier Signal');
grid on;
y=sin(2*pi*fc*t+(mi.*sin(2*pi*fm*t)));%Frequency changing w.r.t Message
subplot(3,1,3);
plot(t,y);
xlabel('Time');
ylabel('Amplitude');
title('FM Signal');
grid on;

How do I plot confidence intervals in MATLAB?

I want to plot some confidence interval graphs in MATLAB but I don't have any idea at all how to do it. I have the data in a .xls file.
Can someone give me a hint, or does anyone know commands for plotting CIs?
After reading numerous threads, here's my attempt.
% Get some random data
x = linspace(0.3, pi-0.3, 10);
Data = sin(x) + randn(1, 10)/10;
Data_sd = 0.1+randn(1,10)/30;
% prepare it for the fill function
x_ax = 1:10;
X_plot = [x_ax, fliplr(x_ax)];
Y_plot = [Data-1.96.*Data_sd, fliplr(Data+1.96.*Data_sd)];
% plot a line + confidence bands
hold on
plot(x_ax, Data, 'blue', 'LineWidth', 1.2)
fill(X_plot, Y_plot , 1,....
'facecolor','blue', ...
'edgecolor','none', ...
'facealpha', 0.3);
hold off
Mostly based on this question: Plotting with transparency
I'm not sure what you meant by confidence intervals graph, but this is an example of how to plot a two-sided 95% CI of a normal distribution:
alpha = 0.05; % significance level
mu = 10; % mean
sigma = 2; % std
cutoff1 = norminv(alpha, mu, sigma);
cutoff2 = norminv(1-alpha, mu, sigma);
x = [linspace(mu-4*sigma,cutoff1), ...
linspace(cutoff1,cutoff2), ...
linspace(cutoff2,mu+4*sigma)];
y = normpdf(x, mu, sigma);
plot(x,y)
xlo = [x(x<=cutoff1) cutoff1];
ylo = [y(x<=cutoff1) 0];
patch(xlo, ylo, 'b')
xhi = [cutoff2 x(x>=cutoff2)];
yhi = [0 y(x>=cutoff2)];
patch(xhi, yhi, 'b')
See e.g. these m-files on Matlab File Exchange:
plot confidence intervals
confplot