FFT and spectrogram on I-Q data

I have an FFT and spectrogram genertated using the follwoing code:
%% fft data generation
Fs = 4e6;
Ns=4e6;
freq=-Fs/2:Fs/Ns:(Fs/2-(Fs/Ns));
amp=10*log10(abs(fft(s(1:Ns),Ns))/Ns);
P=abs(fft(s(1:Ns),Ns))/Ns;
figure(1)
plot((freq+741.5e6),fftshift(P));
%% spectrogram
figure(2);
Nx=4e5;
nsc = floor(Nx/4.5);
nov = floor(nsc/2);
nff = max(256,2^nextpow2(nsc));
spectrogram(s,kaiser(nsc,5.66),nov,nff,Fs, 'MinThreshold',-140);
The output looks like this:
However, I think the spectrogram is wrong:
  1. The frequency axis does not look right
  2. The FFT does not correspond to the spectrogram
Can someone please help me understand the spectrogram with a correct code?
I am using I-Q data (un01)
file = 'un01';
f=fopen(file,'rb');
values = fread(f, Inf,'float');
I=values(1:2:length(values));
Q=values(2:2:length(values));
s=I+1i*Q;

9 Comments

Hi Meghna,
Can you add the 'uni' file to your question using the paper clip icon in the Insert ribbon. Of just save s in .mat file and add it with the paperclip icon?
Why does the code add 741.5e6 to the freq vector when making the plot.
The file is more than 5MB so i cant upload it here.
Why does the code add 741.5e6 to the freq vector when making the plot. -- I want the center frequency of the plot to be 741.5MHz
If "The file" is the uni01 text file, did you try saving s to a .mat file and uploading that? I think the .mat file would be smaller.
The file is a binary file
When i save it as .mat - it is 125MB
This is the link to the .mat file : uns.mat
I can't figure out how to load the data from either form of the file with the links provided.
Does this link work in downloading??
https://drive.google.com/file/d/1IjKADYLo4sBOsPXw7TqPOWfgOaRWqgdo/view?usp=sharing
Please use this code to read the binary file after downloading the data:
file = 'un01';
f=fopen(file,'rb');
values = fread(f, Inf,'float');
I=values(1:2:length(values));
Q=values(2:2:length(values));
s=I+1i*Q;
Is this what the data is supposed to look like? Just want to make sure I'm reading the data properly. There seems to be a disconnect between the number of elements in s, 10387, and the variable Ns=4e6 in the code, which will cause an error in the indexing s(1:Ns) in the computation of amp.
f = fopen(websave('uni01','https://drive.google.com/file/d/1IjKADYLo4sBOsPXw7TqPOWfgOaRWqgdo/view?usp=sharing'),'rb');
values = fread(f, Inf,'float');
I=values(1:2:length(values));
Q=values(2:2:length(values));
s=I+1i*Q;
n = numel(s)
n = 10387
figure
plot(1:n,I,1:n,Q)
file = 'un01';
f=fopen(file,'rb');
values = fread(f, Inf,'float');
I=values(1:2:length(values));
Q=values(2:2:length(values));
s=I+1i*Q;
fs = 4e6;
Ns=4e6; %BW
figure(1)
total_duration = length(s) / fs;
t1=0:(1/fs):total_duration - 1/fs;
figure(1)
plot(t1,s)
>> numel(s)
ans =
35067267
I'm having lots of trouble reading in the data. Every time I try it, values has a different number of elements that aren't close to what you're showing. Don't know why this is a problem. Wish I could be of more help.

Sign in to comment.

Answers (1)

Here is how the power spectrum can be computed & plotted:
f = fopen(websave('uni01','https://drive.google.com/file/d/1IjKADYLo4sBOsPXw7TqPOWfgOaRWqgdo/view?usp=sharing'),'rb');
values = fread(f, Inf,'float');
I=values(1:2:length(values)-1);
Q=values(2:2:length(values));
X=I+1i*Q;
Fs = 4e6;
%% spectrogram
figure(2);
[Power, Freq, Time] = pspectrum(X, Fs, 'spectrogram');
waterfall(Freq,Time, Power')
wtf = gca;
wtf.XDir = 'reverse';
view([30 45])
xlabel('Frequency (Hz)')
ylabel('Time (seconds)')

Asked:

on 12 Oct 2023

Commented:

on 17 Oct 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!