29 views (last 30 days)

I want to draw a frequency spectre of a square signal. Sampling is being performed at 10MHz, i want to see 4 polygons of that frequency

My current code is like this

close all;

%Define number of samples to take

fs = 100e3; %%% 100 kHz

f0 = 10e3; %Hz

%Define signal

t = 0:1/fs:1-1/fs;

dT = length(y);

t1=(0:dT-1)/f0/100;

dolzina=length(t);

y= zeros(1,dolzina);

y(1:fix(dolzina/2))=1;

y=[y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y];

signal = y;

figure(1)

plot(t1, signal);

%Take fourier transform

fftsignal = fft(y);

%apply fftshift to put it in the form

fftsignal = fftshift(fftsignal);

%Next, calculate the frequency axis, which is defined by the sampling rate

f = fs/2*linspace(0,30,fs);

fq = length(f)

%Since the signal is complex, we need to plot the magnitude to get it to

%look right, so we use abs (absolute value)

figure(2)

plot(f, abs(fftsignal));

i get an error, that i can't plot the transformed signal because of vectors not being the same length

can anyone help?

Mathieu NOE
on 14 Jan 2021

hello

there is a much faster method to generate a square signal (using square )

see demo below for signal generation and fft analysis

NB your sampling freq is 100 kHz in the code, not 10 MHz as stated in the post

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% load signal

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% dummy data

Fs = 100e3; % sampling freq

f0 = 10e3; % signal freq

duration = 1; % signal duration (s)

samples = Fs*duration;

t = (0:samples-1)*1/Fs;

% signal = square(2*pi*f0*t); % neg / pos symetrical square wave ( -1 / +1)

signal = 0.5*(square(2*pi*f0*t)+1); % 0 / pos square wave ( 0 / +1)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% FFT parameters

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

NFFT = 10000; %

Overlap = 0.75;

w = hanning(NFFT); % Hanning window / Use the HANN function to get a Hanning window which has the first and last zero-weighted samples.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% display : averaged FFT spectrum

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[freq,fft_spectrum] = myfft_peak(signal, Fs, NFFT, Overlap);

sensor_spectrum_dB = 20*log10(fft_spectrum);% convert to dB scale (ref = 1)

figure(1),plot(freq,sensor_spectrum_dB,'b');grid

title(['Averaged FFT Spectrum / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(freq(2)-freq(1)) ' Hz ']);

xlabel('Frequency (Hz)');ylabel(' dB')

text(locs+.02,pks,num2str(freq(locs)))

function [freq_vector,fft_spectrum] = myfft_peak(signal, Fs, nfft, Overlap)

% FFT peak spectrum of signal (example sinus amplitude 1 = 0 dB after fft).

% Linear averaging

% signal - input signal,

% Fs - Sampling frequency (Hz).

% nfft - FFT window size

% Overlap - buffer overlap % (between 0 and 0.95)

signal = signal(:);

samples = length(signal);

% fill signal with zeros if its length is lower than nfft

if samples<nfft

s_tmp = zeros(nfft,1);

s_tmp((1:samples)) = signal;

signal = s_tmp;

samples = nfft;

end

% window : hanning

window = hanning(nfft);

window = window(:);

% compute fft with overlap

offset = fix((1-Overlap)*nfft);

spectnum = 1+ fix((samples-nfft)/offset); % Number of windows

% % for info is equivalent to :

% noverlap = Overlap*nfft;

% spectnum = fix((samples-noverlap)/(nfft-noverlap)); % Number of windows

% main loop

fft_spectrum = 0;

for i=1:spectnum

start = (i-1)*offset;

sw = signal((1+start):(start+nfft)).*window;

fft_spectrum = fft_spectrum + (abs(fft(sw))*4/nfft); % X=fft(x.*hanning(N))*4/N; % hanning only

end

fft_spectrum = fft_spectrum/spectnum; % to do linear averaging scaling

% one sidded fft spectrum % Select first half

if rem(nfft,2) % nfft odd

select = (1:(nfft+1)/2)';

else

select = (1:nfft/2+1)';

end

fft_spectrum = fft_spectrum(select);

freq_vector = (select - 1)*Fs/nfft;

end

Mathieu NOE
on 14 Jan 2021

hello again

regarding your comment : the signal have lower amplitude of 0 and higher amplitude of 1

is exactly what I suspected , so this is the simplest way to generate it (as in my code) :

signal = 0.5*(square(2*pi*f0*t)+1); % 0 / pos square wave ( 0 / +1)

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

Start Hunting!
## 0 Comments

Sign in to comment.