You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to pass complex double value of array in the workspace to "HDL FFT block-DSP HDL toolbox block" to generate HDL code
3 views (last 30 days)
Show older comments
Fayaz
on 13 Jul 2023
script:
FFT_OUTPUT = fft(ARRAY);% TAKE FFT FOR THE SAMPLES
worspace:
what are the blocks shoulb be added before the FFT block prior to pass the complex double (fixed point value?)
Accepted Answer
Bharath Venkataraman
on 14 Jul 2023
I assume the data you are showing is for a 512 point FFT and you have 20 frames of data.
I would first serialize this data using reshape the data:
ARRAY2 = ARRAY(:);
Check to make sure that ARRAY2(1:10) has the right order of data you expect to send in to the FFT.
The attached model uses the Signal From Workspace block to get the data into the Simulink model and uses the data type conversion block to convert it to fixed point before feeding it to the FFT block. I have the set teh input valid to be true all the time - this is something you can modify if needed.
The sample time for the Signal From Workspace is set to 1sec, so I run the model 512*20 seconds to get the 20 frames of data in. Because of the FFT latency, you will get about 18-19 frames of data out. To get all the 20 frames out, you can add another 600 seconds to the Stop Time.
Hope this helps,
Bharath
16 Comments
Fayaz
on 15 Jul 2023
THANK YOU so much sir. once aganin thanx for sparing your time for me. i will go throuh the steps that you have mentioned.
can i contact you to clarify some doubts? i would be grateful to you
Fayaz
on 15 Jul 2023
sir, if i can add a "To workspace" block at output of "data conversion to Double", i should a get the same output coming in the script ouput in the workspace from " FFT_OUTPUT = fft(ARRAY)", isnt it ?
Bharath Venkataraman
on 15 Jul 2023
Yes, that is largely true, but because of fixed point and implementation differences, the values will not be exact. Attached is a model and script that compare the two implementations. Run the script and you will see some differences, but they are not large.
Fayaz
on 16 Jul 2023
>> fft_compare
Unrecognized function or variable 'hdlfft_data'.
Error in fft_compare (line 14)
hdlfft_valid_data = hdlfft_data(hdlfft_valid);
############## I AM GETTING AN ERROR , It seems that hdfft_data might be a custom function or a part of a library specific to your environment. it is not supporting for my r2023a
Bharath Venkataraman
on 16 Jul 2023
I have updated my answer above - the hdlfft_data needed to be gathered from the Simulink model output logsout. This is now done in line 13.
Fayaz
on 17 Jul 2023
thank you so much sir. i have a question about the code, in line 3, you have converted the double to signed fixed point value and again in the simulink, you are using a convertion to fi(1,10,8). We can pass the double value to "signla from workspace" and in the converter, we can directly convert to fi(1,10,8) or else we canvert to fixed pint (1,10,8) directly in the script and pass it the"signal from workspace".if you dont mind, can you explain me the any special reason for it
Bharath Venkataraman
on 17 Jul 2023
You are right - we do not need to convert the values two times. You can remove one of the conversions.
Fayaz
on 17 Jul 2023
Ok,,,,,,,,,,
i did the same you have done. I used the conversion as fi(1,18,16) as this word length is preferable for FPGA implementation ( which is mentioned by the mathwork tutorial). I got real and imag errors respectively aroung 4. and 5. ,,, that is considerabely a huge value. what can i do for that sir?
Bharath Venkataraman
on 17 Jul 2023
I removed the data type conversion from the Simulink model and left it in line 3 of the script. Changing the data type to 1,18,16 and running the fft_compare script give max differences of ~2e-4.
Fayaz
on 18 Jul 2023
Still im getting the same answer. which is,
Max real diff is 4.434067
Max imag diff is 4.996368
my "fft_ou1" has big real value:
real(max(fft_out1)) = 4.366128658879322
so my point is, there should be a real value in the "hdlfft_valid_data_frame1" near to real part of "fft_out1", isnt it?but it is,"real(max(hdlfft_valid_data_frame1)) = -0.186767578125000"
imaginary part is fishy:
imag(max(fft_out1)) = 2.438102782136530
imag(max(hdlfft_valid_data_frame1)) = 0.608795166015625
but the difference shows around 4.996368, it should be something 2.438102782136530 - 0.608795166015625 =1.8
########## i will attach my files, if you dont mind can you look over it, and help me to fix the bug, i would be a huge help
Fayaz
on 18 Jul 2023
#############SIR ANOTHER HUGE HELP SIR.................
Sir,you are the only one responding my questions and helping me. You have given me guidance for the "NCO" opration process to make it work, but still i have confusion and not able to get the correct block system for that case of frequency shifting. I studied and tried up with applying "LUT-look up table". I dont know it is using a LUT is correct or not for the process, according the MATLAB script of frequency shifting,
fs_shift1 = 4;
fs_u = 30.72;
Reverse_NCO_out_20 = combined_carrier_shifted_20.*exp(-1i*2*pi*fs_shift1/fs_u*(1:length(combined_carrier_shifted_20)));
the length of "combined_carrier_shifted_20" is multiplied with phase, so in that case do we have to add a LUT? i dont know.
Sir im in the around the corner of submission of my undergraduate project and as im an undergraduate, have lack of knowledge about the MATLAB and not interacted much with it, not aware of advance things in MATALB because started use it for the final year project only. Its difficult to get good practice and knowledge within a short period with those high technical base documents available in matlab and cources are too expensive. So, sir kindly request you to consider and i would be much more grateful to you
Bharath Venkataraman
on 21 Jul 2023
Edited: Bharath Venkataraman
on 21 Jul 2023
I redid the calculation to calculate the fft on the multi-channel data. The differences are much smaller now.
Fayaz
on 25 Jul 2023
Edited: Fayaz
on 30 Jul 2023
% array = sin(0:2*pi/128:2*pi-2*pi/128); % 1x128 array
% array1 = repmat(array,1,4); % 1x512
array1 = fi(ARRAY,1,18,16); % fixed point
array2 = array1(:);
% Behavioral FFT
fft_out1 = fft(double(array1));
fft_out2 = fft_out1(:);
% Run HDL FFT model
open_system("FFTHDL_model.slx")
sim("FFTHDL_model.slx")
% Get output of HDLFFT model
hdlfft_data = squeeze(logsout.getElement('fft_data_out').Values.Data);
hdlfft_valid = squeeze(logsout.getElement('fft_vld_out').Values.Data);
hdlfft_valid_data = hdlfft_data(hdlfft_valid);
% Difference between HDL and behavioral output
last_idx = 10240;
fft_diff = fft_out2(1:last_idx)-hdlfft_valid_data(1:last_idx);
fprintf("Max real diff is %f\n", max(real(fft_diff)));
fprintf("Max imag diff is %f\n", max(imag(fft_diff)));
fft_diff = fft_out2(1:last_idx)-hdlfft_valid_data(1:last_idx);
in the above line you are calculating the difference etween 'fft_out2' which is doubel precision floating point value and 'hdlfft_valid_data' is fixed point value. Is that correct, two diffenret data types are there, isnt it?
please consider this ........................................
Fayaz
on 25 Jul 2023
#############SIR ANOTHER HUGE HELP SIR.................
Sir,you are the only one responding my questions and helping me. You have given me guidance for the "NCO" opration process to make it work, but still i have confusion and not able to get the correct block system for that case of frequency shifting. I studied and tried up with applying "LUT-look up table". I dont know it is using a LUT is correct or not for the process, according the MATLAB script of frequency shifting,
fs_shift1 = 4;
fs_u = 30.72;
Reverse_NCO_out_20 = combined_carrier_shifted_20.*exp(-1i*2*pi*fs_shift1/fs_u*(1:length(combined_carrier_shifted_20)));
the length of "combined_carrier_shifted_20" is multiplied with phase, so in that case do we have to add a LUT? i dont know.
Sir im in the around the corner of submission of my undergraduate project and as im an undergraduate, have lack of knowledge about the MATLAB and not interacted much with it, not aware of advance things in MATALB because started use it for the final year project only. Its difficult to get good practice and knowledge within a short period with those high technical base documents available in matlab and cources are too expensive. So, sir kindly request you to consider and i would be much more grateful to you
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)